不能在SQL视图中使用declare或temp表,请

时间:2016-04-08 03:47:10

标签: sql-server sql-server-2012 ssms sqlclr sql-view

我需要在SQL 2012中创建一个视图,该脚本在SSMS中作为独立运行但需要转换为视图,但脚本使用CLR生成密钥数据。所以到目前为止,我们已经将CLR结果放入临时表并从那里加入。由于视图中不允许使用临时表,我们已经使用了表的变量,但这会在声明部分周围引发错误。我们显然已经尝试将其作为存储过程运行,但是无法从视图中调用它...任何建议都将受到赞赏。

这是工作代码

 declare @idList IdList
 INSERT INTO @idList SELECT distinct RT_ID FROM [RT]
 DECLARE @CLRs TABLE (
 RT_ID nvarchar(19)
,rowindex nvarchar(20)
,Exp1 nvarchar(20)
,Exp2 nvarchar(20)   )

INSERT INTO @CLRs
SELECT [IDLIST].[id] as RT_ID
,AD.ROWINDEX
,dbo.fnPropercase(Contact) as Exp1
,dbo.fnPropercase(Contact2) as Exp2
from @idList [IDLIST]
LEFT OUTER JOIN [CLR DETAILS](@idList,1) [AD] on [IDLIST].[id] = [AD].RT_ID
INNER JOIN CONTACTS CS on CS.CONTACTS_ID= AD.RT_CONT_ID
SELECT * FROM @CLRs

显然这是一个简化的脚本,但目的仍然是

三江源。

1 个答案:

答案 0 :(得分:1)

CTE就是答案。

$table->dropUnique('customer_plans_code_unique');

以上应该是您的视图定义。正如您所提到的那样,这是一个简化的案例,您可以随意添加更多WITH IdList AS (SELECT distinct RT_ID FROM [RT]), CLRs AS (SELECT [IDLIST].[id] AS RT_ID ,AD.ROWINDEX ,dbo.fnPropercase(Contact) AS Exp1 ,dbo.fnPropercase(Contact2) AS Exp2 FROM IdList [IDLIST] LEFT OUTER JOIN [CLR DETAILS](IdList,1) [AD] ON [IDLIST].[id] = [AD].RT_ID INNER JOIN CONTACTS CS ON CS.CONTACTS_ID = AD.RT_CONT_ID) SELECT * FROM CLRs 并按照您的意愿加入它们!

文档:MDSN - CTEs