在 SQL 中转置列和行的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13372276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple way to transpose columns and rows in SQL?
提问by edezzie
How do I simply switch columns with rows in SQL? Is there any simple command to transpose?
如何在 SQL 中简单地用行切换列?有什么简单的命令可以转置吗?
ie turn this result:
即转这个结果:
Paul | John | Tim | Eric
Red 1 5 1 3
Green 8 4 3 5
Blue 2 2 9 1
into this:
进入这个:
Red | Green | Blue
Paul 1 8 2
John 5 4 2
Tim 1 3 9
Eric 3 5 1
PIVOT
seems too complex for this scenario.
PIVOT
对于这种情况,似乎太复杂了。
回答by Taryn
There are several ways that you can transform this data. In your original post, you stated that PIVOT
seems too complex for this scenario, but it can be applied very easily using both the UNPIVOT
and PIVOT
functions in SQL Server.
有多种方法可以转换此数据。在您的原始帖子中,您表示这PIVOT
对于这种情况来说似乎太复杂了,但是使用SQL Server 中的UNPIVOT
和PIVOT
函数可以非常轻松地应用它。
However, if you do not have access to those functions this can be replicated using UNION ALL
to UNPIVOT
and then an aggregate function with a CASE
statement to PIVOT
:
但是,如果您无权访问这些函数,则可以使用UNION ALL
toUNPIVOT
和带有CASE
语句 to的聚合函数进行复制PIVOT
:
Create Table:
创建表:
CREATE TABLE yourTable([color] varchar(5), [Paul] int, [John] int, [Tim] int, [Eric] int);
INSERT INTO yourTable
([color], [Paul], [John], [Tim], [Eric])
VALUES
('Red', 1, 5, 1, 3),
('Green', 8, 4, 3, 5),
('Blue', 2, 2, 9, 1);
Union All, Aggregate and CASE Version:
Union All、Aggregate 和 CASE 版本:
select name,
sum(case when color = 'Red' then value else 0 end) Red,
sum(case when color = 'Green' then value else 0 end) Green,
sum(case when color = 'Blue' then value else 0 end) Blue
from
(
select color, Paul value, 'Paul' name
from yourTable
union all
select color, John value, 'John' name
from yourTable
union all
select color, Tim value, 'Tim' name
from yourTable
union all
select color, Eric value, 'Eric' name
from yourTable
) src
group by name
The UNION ALL
performs the UNPIVOT
of the data by transforming the columns Paul, John, Tim, Eric
into separate rows. Then you apply the aggregate function sum()
with the case
statement to get the new columns for each color
.
的UNION ALL
执行与UNPIVOT
通过变换列中的数据Paul, John, Tim, Eric
进行分离。然后将聚合函数sum()
与case
语句一起应用以获取每个 的新列color
。
Unpivot and Pivot Static Version:
Unpivot 和 Pivot 静态版本:
Both the UNPIVOT
and PIVOT
functions in SQL server make this transformation much easier. If you know all of the values that you want to transform, you can hard-code them into a static version to get the result:
SQL Server 中的UNPIVOT
和PIVOT
函数使这种转换更加容易。如果您知道要转换的所有值,则可以将它们硬编码为静态版本以获得结果:
select name, [Red], [Green], [Blue]
from
(
select color, name, value
from yourtable
unpivot
(
value for name in (Paul, John, Tim, Eric)
) unpiv
) src
pivot
(
sum(value)
for color in ([Red], [Green], [Blue])
) piv
The inner query with the UNPIVOT
performs the same function as the UNION ALL
. It takes the list of columns and turns it into rows, the PIVOT
then performs the final transformation into columns.
带有 的内部查询UNPIVOT
执行与 相同的功能UNION ALL
。它获取列列表并将其转换为行,PIVOT
然后执行最终转换为列。
Dynamic Pivot Version:
动态枢轴版本:
If you have an unknown number of columns (Paul, John, Tim, Eric
in your example) and then an unknown number of colors to transform you can use dynamic sql to generate the list to UNPIVOT
and then PIVOT
:
如果您有未知数量的列(Paul, John, Tim, Eric
在您的示例中),然后有未知数量的颜色要转换,您可以使用动态 sql 生成列表UNPIVOT
,然后PIVOT
:
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@colsPivot as NVARCHAR(MAX)
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('yourtable') and
C.name <> 'color'
for xml path('')), 1, 1, '')
select @colsPivot = STUFF((SELECT ','
+ quotename(color)
from yourtable t
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query
= 'select name, '+@colsPivot+'
from
(
select color, name, value
from yourtable
unpivot
(
value for name in ('+@colsUnpivot+')
) unpiv
) src
pivot
(
sum(value)
for color in ('+@colsPivot+')
) piv'
exec(@query)
The dynamic version queries both yourtable
and then the sys.columns
table to generate the list of items to UNPIVOT
and PIVOT
. This is then added to a query string to be executed. The plus of the dynamic version is if you have a changing list of colors
and/or names
this will generate the list at run-time.
动态版本先查询这两个表yourtable
,然后sys.columns
生成到UNPIVOT
和的项目列表PIVOT
。然后将其添加到要执行的查询字符串中。动态版本的优点是如果您有一个更改列表colors
和/或names
这将在运行时生成列表。
All three queries will produce the same result:
所有三个查询都会产生相同的结果:
| NAME | RED | GREEN | BLUE |
-----------------------------
| Eric | 3 | 5 | 1 |
| John | 5 | 4 | 2 |
| Paul | 1 | 8 | 2 |
| Tim | 1 | 3 | 9 |
回答by RichardTheKiwi
This normally requires you to know ALL the column AND row labels beforehand. As you can see in the query below, the labels are all listed in their entirely in both the UNPIVOT and the (re)PIVOT operations.
这通常要求您事先知道所有的列和行标签。正如您在下面的查询中看到的那样,标签在 UNPIVOT 和 (re)PIVOT 操作中都完全列出。
MS SQL Server 2012 Schema Setup:
MS SQL Server 2012 架构设置:
create table tbl (
color varchar(10), Paul int, John int, Tim int, Eric int);
insert tbl select
'Red' ,1 ,5 ,1 ,3 union all select
'Green' ,8 ,4 ,3 ,5 union all select
'Blue' ,2 ,2 ,9 ,1;
Query 1:
查询 1:
select *
from tbl
unpivot (value for name in ([Paul],[John],[Tim],[Eric])) up
pivot (max(value) for color in ([Red],[Green],[Blue])) p
结果:
| NAME | RED | GREEN | BLUE |
-----------------------------
| Eric | 3 | 5 | 1 |
| John | 5 | 4 | 2 |
| Paul | 1 | 8 | 2 |
| Tim | 1 | 3 | 9 |
Additional Notes:
补充说明:
- Given a table name, you can determine all the column names from sys.columnsor FOR XML trickery using local-name().
- You can also build up the list of distinct colors (or values for one column) using FOR XML.
- The above can be combined into a dynamic sql batch to handle any table.
- 给定一个表名,您可以使用local-name()从sys.columns或 FOR XML技巧中确定所有列名。
- 您还可以使用 FOR XML 构建不同颜色(或一列的值)的列表。
- 以上可以组合成一个动态的sql批处理来处理任何表。
回答by MikeS
I'd like to point out few more solutions to transposing columns and rows in SQL.
我想指出更多在 SQL 中转置列和行的解决方案。
The first one is - using CURSOR. Although the general consensus in the professional community is to stay away from SQL Server Cursors, there are still instances whereby the use of cursors is recommended. Anyway, Cursors present us with another option to transpose rows into columns.
第一个是 - 使用 CURSOR。尽管专业社区的普遍共识是远离 SQL Server 游标,但仍有一些实例建议使用游标。无论如何,Cursors 为我们提供了另一种将行转换为列的选项。
Vertical expansion
Similar to the PIVOT, the cursor has the dynamic capability to append more rows as your dataset expands to include more policy numbers.
Horizontal expansion
Unlike the PIVOT, the cursor excels in this area as it is able to expand to include newly added document, without altering the script.
Performance breakdown
The major limitation of transposing rows into columns using CURSOR is a disadvantage that is linked to using cursors in general – they come at significant performance cost. This is because the Cursor generates a separate query for each FETCH NEXT operation.
纵向扩展
与 PIVOT 类似,当您的数据集扩展以包含更多政策编号时,游标具有附加更多行的动态能力。
横向扩展
与 PIVOT 不同,光标在该区域中表现出色,因为它能够扩展以包含新添加的文档,而无需更改脚本。
性能细分
使用 CURSOR 将行转换为列的主要限制是与一般使用游标相关的一个缺点——它们会带来显着的性能成本。这是因为 Cursor 为每个 FETCH NEXT 操作生成一个单独的查询。
Another solution of transposing rows into columns is by using XML.
另一种将行转换为列的解决方案是使用 XML。
The XML solution to transposing rows into columns is basically an optimal version of the PIVOT in that it addresses the dynamic column limitation.
将行转换为列的 XML 解决方案基本上是 PIVOT 的最佳版本,因为它解决了动态列限制。
The XML version of the script addresses this limitation by using a combination of XML Path, dynamic T-SQL and some built-in functions (i.e. STUFF, QUOTENAME).
该脚本的 XML 版本通过使用 XML 路径、动态 T-SQL 和一些内置函数(即 STUFF、QUOTENAME)的组合解决了这一限制。
Vertical expansion
Similar to the PIVOT and the Cursor, newly added policies are able to be retrieved in the XML version of the script without altering the original script.
Horizontal expansion
Unlike the PIVOT, newly added documents can be displayed without altering the script.
Performance breakdown
In terms of IO, the statistics of the XML version of the script is almost similar to the PIVOT – the only difference is that the XML has a second scan of dtTranspose table but this time from a logical read – data cache.
纵向扩展
与 PIVOT 和 Cursor 类似,新添加的策略可以在脚本的 XML 版本中检索,而无需更改原始脚本。
横向扩展
与 PIVOT 不同的是,无需更改脚本即可显示新添加的文档。
性能细分
在 IO 方面,XML 版本脚本的统计数据几乎与 PIVOT 相似——唯一的区别是 XML 对 dtTranspose 表进行了第二次扫描,但这次是从逻辑读取 - 数据缓存。
You can find some more about these solutions (including some actual T-SQL exmaples) in this article: https://www.sqlshack.com/multiple-options-to-transposing-rows-into-columns/
您可以在本文中找到有关这些解决方案的更多信息(包括一些实际的 T-SQL 示例):https://www.sqlshack.com/multiple-options-to-transposing-rows-into-columns/
回答by Paco Zarate
Based on this solutionfrom bluefeethere is a stored procedure that uses dynamic sql to generate the transposed table. It requires that all the fields are numeric except for the transposed column (the column that will be the header in the resulting table):
基于bluefeet 的这个解决方案,这里是一个使用动态 sql 生成转置表的存储过程。它要求除了转置列(将成为结果表中的标题的列)之外的所有字段都是数字:
/****** Object: StoredProcedure [dbo].[SQLTranspose] Script Date: 11/10/2015 7:08:02 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Paco Zarate
-- Create date: 2015-11-10
-- Description: SQLTranspose dynamically changes a table to show rows as headers. It needs that all the values are numeric except for the field using for transposing.
-- Parameters: @TableName - Table to transpose
-- @FieldNameTranspose - Column that will be the new headers
-- Usage: exec SQLTranspose <table>, <FieldToTranspose>
-- =============================================
ALTER PROCEDURE [dbo].[SQLTranspose]
-- Add the parameters for the stored procedure here
@TableName NVarchar(MAX) = '',
@FieldNameTranspose NVarchar(MAX) = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@queryPivot AS NVARCHAR(MAX),
@colsPivot as NVARCHAR(MAX),
@columnToPivot as NVARCHAR(MAX),
@tableToPivot as NVARCHAR(MAX),
@colsResult as xml
select @tableToPivot = @TableName;
select @columnToPivot = @FieldNameTranspose
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id(@tableToPivot) and
C.name <> @columnToPivot
for xml path('')), 1, 1, '')
set @queryPivot = 'SELECT @colsResult = (SELECT '',''
+ quotename('+@columnToPivot+')
from '+@tableToPivot+' t
where '+@columnToPivot+' <> ''''
FOR XML PATH(''''), TYPE)'
exec sp_executesql @queryPivot, N'@colsResult xml out', @colsResult out
select @colsPivot = STUFF(@colsResult.value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query
= 'select name, rowid, '+@colsPivot+'
from
(
select '+@columnToPivot+' , name, value, ROW_NUMBER() over (partition by '+@columnToPivot+' order by '+@columnToPivot+') as rowid
from '+@tableToPivot+'
unpivot
(
value for name in ('+@colsUnpivot+')
) unpiv
) src
pivot
(
sum(value)
for '+@columnToPivot+' in ('+@colsPivot+')
) piv
order by rowid'
exec(@query)
END
You can test it with the table provided with this command:
您可以使用此命令提供的表对其进行测试:
exec SQLTranspose 'yourTable', 'color'
回答by mr_eclair
I'm doing UnPivot
first and storing the results in CTE
and using the CTE
in Pivot
operation.
我UnPivot
首先做并将结果存储在CTE
并使用CTE
inPivot
操作中。
with cte as
(
select 'Paul' as Name, color, Paul as Value
from yourTable
union all
select 'John' as Name, color, John as Value
from yourTable
union all
select 'Tim' as Name, color, Tim as Value
from yourTable
union all
select 'Eric' as Name, color, Eric as Value
from yourTable
)
select Name, [Red], [Green], [Blue]
from
(
select *
from cte
) as src
pivot
(
max(Value)
for color IN ([Red], [Green], [Blue])
) as Dtpivot;
回答by Jonathan Harris
Adding to @Paco Zarate's terrific answer above, if you want to transpose a table which has multiple types of columns, then add this to the end of line 39, so it only transposes int
columns:
添加到@Paco Zarate 上面的绝妙答案中,如果您想转置具有多种类型列的表,请将其添加到第 39 行的末尾,因此它只转置int
列:
and C.system_type_id = 56 --56 = type int
Here is the full query that is being changed:
这是正在更改的完整查询:
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id(@tableToPivot) and
C.name <> @columnToPivot and C.system_type_id = 56 --56 = type int
for xml path('')), 1, 1, '')
To find other system_type_id
's, run this:
要查找 other system_type_id
,请运行以下命令:
select name, system_type_id from sys.types order by name
回答by Mahmood Khezrian
This way Convert all Data From Filelds(Columns) In Table To Record (Row).
这种方式将所有数据从表中的文件(列)转换为记录(行)。
Declare @TableName [nvarchar](128)
Declare @ExecStr nvarchar(max)
Declare @Where nvarchar(max)
Set @TableName = 'myTableName'
--Enter Filtering If Exists
Set @Where = ''
--Set @ExecStr = N'Select * From '+quotename(@TableName)+@Where
--Exec(@ExecStr)
Drop Table If Exists #tmp_Col2Row
Create Table #tmp_Col2Row
(Field_Name nvarchar(128) Not Null
,Field_Value nvarchar(max) Null
)
Set @ExecStr = N' Insert Into #tmp_Col2Row (Field_Name , Field_Value) '
Select @ExecStr += (Select N'Select '''+C.name+''' ,Convert(nvarchar(max),'+quotename(C.name) + ') From ' + quotename(@TableName)+@Where+Char(10)+' Union All '
from sys.columns as C
where (C.object_id = object_id(@TableName))
for xml path(''))
Select @ExecStr = Left(@ExecStr,Len(@ExecStr)-Len(' Union All '))
--Print @ExecStr
Exec (@ExecStr)
Select * From #tmp_Col2Row
Go
回答by Elidio Marquina
I like to share the code i'm using to transpose a splited text based on +bluefeet answer. In this aproach i'm implemented as a procedure in MS SQL 2005
我喜欢分享我用来根据 +bluefeet 答案转置拆分文本的代码。在这种方法中,我作为 MS SQL 2005 中的过程实现
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: ELD.
-- Create date: May, 5 2016.
-- Description: Transpose from rows to columns the user split function.
-- =============================================
CREATE PROCEDURE TransposeSplit @InputToSplit VARCHAR(8000)
,@Delimeter VARCHAR(8000) = ','
AS
BEGIN
SET NOCOUNT ON;
DECLARE @colsUnpivot AS NVARCHAR(MAX)
,@query AS NVARCHAR(MAX)
,@queryPivot AS NVARCHAR(MAX)
,@colsPivot AS NVARCHAR(MAX)
,@columnToPivot AS NVARCHAR(MAX)
,@tableToPivot AS NVARCHAR(MAX)
,@colsResult AS XML
SELECT @tableToPivot = '#tempSplitedTable'
SELECT @columnToPivot = 'col_number'
CREATE TABLE #tempSplitedTable (
col_number INT
,col_value VARCHAR(8000)
)
INSERT INTO #tempSplitedTable (
col_number
,col_value
)
SELECT ROW_NUMBER() OVER (
ORDER BY (
SELECT 100
)
) AS RowNumber
,item
FROM [DB].[ESCHEME].[fnSplit](@InputToSplit, @Delimeter)
SELECT @colsUnpivot = STUFF((
SELECT ',' + quotename(C.NAME)
FROM [tempdb].sys.columns AS C
WHERE C.object_id = object_id('tempdb..' + @tableToPivot)
AND C.NAME <> @columnToPivot
FOR XML path('')
), 1, 1, '')
SET @queryPivot = 'SELECT @colsResult = (SELECT '',''
+ quotename(' + @columnToPivot + ')
from ' + @tableToPivot + ' t
where ' + @columnToPivot + ' <> ''''
FOR XML PATH(''''), TYPE)'
EXEC sp_executesql @queryPivot
,N'@colsResult xml out'
,@colsResult OUT
SELECT @colsPivot = STUFF(@colsResult.value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @query = 'select name, rowid, ' + @colsPivot + '
from
(
select ' + @columnToPivot + ' , name, value, ROW_NUMBER() over (partition by ' + @columnToPivot + ' order by ' + @columnToPivot + ') as rowid
from ' + @tableToPivot + '
unpivot
(
value for name in (' + @colsUnpivot + ')
) unpiv
) src
pivot
(
MAX(value)
for ' + @columnToPivot + ' in (' + @colsPivot + ')
) piv
order by rowid'
EXEC (@query)
DROP TABLE #tempSplitedTable
END
GO
I'm mixing this solution with the information about howto order rows without order by (SQLAuthority.com) and the split function on MSDN (social.msdn.microsoft.com)
我正在将此解决方案与有关如何在没有 order by ( SQLAuthority.com) 和 MSDN ( social.msdn.microsoft.com)上的拆分功能的情况下订购行的信息混合
When you execute the prodecure
当你执行 prodeure
DECLARE @RC int
DECLARE @InputToSplit varchar(MAX)
DECLARE @Delimeter varchar(1)
set @InputToSplit = 'hello|beautiful|world'
set @Delimeter = '|'
EXECUTE @RC = [TransposeSplit]
@InputToSplit
,@Delimeter
GO
you obtaint the next result
你得到下一个结果
name rowid 1 2 3
col_value 1 hello beautiful world
回答by CraigD
I was able to use Paco Zarate's solution and it works beautifully. I did have to add one line ("SET ANSI_WARNINGS ON"), but that may be something unique to the way I used it or called it. There is a problem with my usage and I hope someone can help me with it:
我能够使用 Paco Zarate 的解决方案,而且效果很好。我确实必须添加一行(“SET ANSI_WARNINGS ON”),但这可能是我使用或调用它的方式所独有的。我的使用有问题,希望有人能帮我解决:
The solution works only with an actual SQL table. I tried it with a temporary table and also an in-memory (declared) table but it doesn't work with those. So in my calling code I create a table on my SQL database and then call SQLTranspose. Again, it works great. It's just what I want. Here's my problem:
该解决方案仅适用于实际的 SQL 表。我尝试使用临时表和内存中(声明的)表,但它不适用于这些表。所以在我的调用代码中,我在我的 SQL 数据库上创建了一个表,然后调用 SQLTranspose。再次,它工作得很好。这正是我想要的。这是我的问题:
In order for the overall solution to be truly dynamic I need to create that table where I temporarily store the prepared information that I'm sending to SQLTranspose "on the fly", and then delete that table once SQLTranspose is called. The table deletion is presenting a problem with my ultimate implementation plan. The code needs to run from an end-user application (a button on a Microsoft Access form/menu). When I use this SQL process (create a SQL table, call SQLTranspose, delete SQL table) the end user application hits an error because the SQL account used does not have the rights to drop a table.
为了使整体解决方案真正具有动态性,我需要创建该表,在该表中临时存储我“即时”发送到 SQLTranspose 的准备好的信息,然后在调用 SQLTranspose 后删除该表。表删除给我的最终实施计划带来了问题。代码需要从最终用户应用程序(Microsoft Access 窗体/菜单上的按钮)运行。当我使用此 SQL 进程(创建 SQL 表、调用 SQLTranspose、删除 SQL 表)时,最终用户应用程序遇到错误,因为所使用的 SQL 帐户没有删除表的权限。
So I figure there are a few possible solutions:
所以我认为有几种可能的解决方案:
Find a way to make SQLTranspose work with a temporary table or a declared table variable.
Figure out another method for the transposition of rows and columns that doesn't require an actual SQL table.
Figure out an appropriate method of allowing the SQL account used by my end users to drop a table. It's a single shared SQL account coded into my Access application. It appears that permission is a dbo-type privilege that cannot be granted.
找到一种方法使 SQLTranspose 与临时表或声明的表变量一起工作。
找出另一种不需要实际 SQL 表的行和列换位方法。
找出允许我的最终用户使用的 SQL 帐户删除表的适当方法。它是编码到我的 Access 应用程序中的单个共享 SQL 帐户。权限似乎是无法授予的 dbo 类型权限。
I recognize that some of this may warrant another, separate thread and question. However, since there is a possibility that one solution may be simply a different way to do the transposing of rows and columns I'll make my first post here in this thread.
我认识到其中一些可能需要另一个单独的线程和问题。但是,由于一个解决方案可能只是一种不同的行和列转置方式,因此我将在此线程中发表我的第一篇文章。
EDIT: I also did replace sum(value) with max(value) in the 6th line from the end, as Paco suggested.
编辑:正如 Paco 建议的那样,我也确实在最后的第 6 行用 max(value) 替换了 sum(value)。
EDIT:
编辑:
I figured out something that works for me. I don't know if it's the bestanswer or not.
我想出了一些对我有用的东西。我不知道这是否是最佳答案。
I have a read-only user account that is used to execute strored procedures and therefore generate reporting output from a database. Since the SQLTranspose function I created will only work with a "legitimate" table (not a declared table and not a temporary table) I had to figure out a way for a read-only user account to create (and then later delete) a table.
我有一个只读用户帐户,用于执行存储过程,因此从数据库生成报告输出。由于我创建的 SQLTranspose 函数仅适用于“合法”表(不是已声明的表,也不是临时表),因此我必须想办法让只读用户帐户创建(然后删除)表.
I reasoned that for my purposes it's okay for the user account to be allowed to create a table. The user still could not delete the table though. My solution was to create a schema where the user account is authorized. Then whenever I create, use, or delete that table refer it with the schema specified.
我的理由是,出于我的目的,允许用户帐户创建表是可以的。尽管如此,用户仍然无法删除该表。我的解决方案是创建一个授权用户帐户的架构。然后,每当我创建、使用或删除该表时,都会使用指定的架构引用它。
I first issued this command from a 'sa' or 'sysadmin' account: CREATE SCHEMA ro AUTHORIZATION
我首先从“sa”或“sysadmin”帐户发出此命令:CREATE SCHEMA ro AUTHORIZATION
When any time I refer to my "tmpoutput" table I specify it like this example:
每当我提到我的“tmpoutput”表时,我都会像这个例子一样指定它:
drop table ro.tmpoutput
删除表 ro.tmpoutput