声明标量变量

时间:2017-01-11 17:15:02

标签: sql sql-server tsql

我正在编写一个失败的错误

的动态SQL语句
  

必须声明标量变量" @@ config"

我试图破解代码并确认以下代码确实返回结果:

Declare @@sql as nvarchar(max);
Declare @SourceDatabase as nvarchar(10);
Declare @DestDatabase as nvarchar(10);
Declare @@Harray as nvarchar(max) ;

Set @SourceDatabase = 'D_EXP_CPM';
Set @DestDatabase = 'T_EXP_CPM';

set @@sql = 'SELECT B.Hierarchy_SKey AS ''NEW'', A.Hierarchy_SKey AS ''OLD''
             INTO ##TEMPHCONVERT
             FROM ' + @SourceDatabase + '.[dbo].[DIM_HIERARCHY] A 
             INNER JOIN ' + @DestDatabase + '.[dbo].[DIM_HIERARCHY] B 
                   ON A.Hierarchy_Desc = B.Hierarchy_Desc
             WHERE A.Hierarchy_SKey in (' + @@Harray + ');'

exec (@@sql);

Declare @@config as NVARCHAR(MAX);

SELECT @@config = COALESCE(@@config + ', H' + cast(A.[NEW] as nvarchar), 'H' + cast(A.[NEW] as nvarchar)) 
FROM ##TEMPHCONVERT A;


Print @@config

返回值:" H15,H16,H17,H18"

当插入动态SQL语句时,虽然我收到了错误...

Declare @SourceDatabase as nvarchar(10)
Declare @DestDatabase as nvarchar(10)
Declare @@sql as nvarchar(max) --variable to hold sql statements for this stored proc
Declare @@Harray as nvarchar(max) -- variable to hold hierarchy array elements from source

Set @SourceDatabase = 'D_EXP_CPM';
Set @DestDatabase = 'T_EXP_CPM';

Declare @@config as NVARCHAR(MAX);

set @@sql = 
'SELECT 
B.Hierarchy_SKey AS ''NEW'', A.Hierarchy_SKey AS ''OLD''
INTO ##TEMPHCONVERT
FROM ' + @SourceDatabase + '.[dbo].[DIM_HIERARCHY] A 
INNER JOIN ' + @DestDatabase + '.[dbo].[DIM_HIERARCHY] B 
ON A.Hierarchy_Desc = B.Hierarchy_Desc

WHERE A.Hierarchy_SKey in (' + @@Harray + ');'

exec (@@sql);

set @@sql = '
UPDATE A SET A.[Value_Text] = (SELECT @@config = COALESCE(@@config + '', H'' + cast(A.[NEW] as nvarchar), ''H'' + cast(A.[NEW] as nvarchar)) 
FROM ##TEMPHCONVERT A)
FROM ' + @DestDatabase + '.[dbo].[DIM_CONFIG] A ON A.[Config_Desc] = ''JBCODE FTE Map Hierarchy  List'' ;' 

exec (@@sql);

注意我已经在中间删除了相当多的代码,以使帖子尽可能短。任何人都能解释为什么我会收到错误吗?

我尝试在两个地方声明动态SQL语句中的变量,但都不允许代码运行。我想也许变量需要以某种方式传递,但我不知道如何通过子查询来做到这一点。

3 个答案:

答案 0 :(得分:0)

将@@ config声明放在@@ sql字符串中:

Declare @SourceDatabase as nvarchar(10)
    Declare @DestDatabase as nvarchar(10)
    Declare @@sql as nvarchar(max) --variable to hold sql statements for this stored proc
    Declare @@Harray as nvarchar(max) -- variable to hold hierarchy array elements from source
    Set @SourceDatabase = 'D_EXP_CPM';
    Set @DestDatabase = 'T_EXP_CPM';
set @@sql = 
'SELECT 
B.Hierarchy_SKey AS ''NEW'', A.Hierarchy_SKey AS ''OLD''
INTO ##TEMPHCONVERT
FROM ' + @SourceDatabase + '.[dbo].[DIM_HIERARCHY] A 
INNER JOIN ' + @DestDatabase + '.[dbo].[DIM_HIERARCHY] B 
ON A.Hierarchy_Desc = B.Hierarchy_Desc

WHERE A.Hierarchy_SKey in (' + @@Harray + ');'

exec (@@sql);

set @@sql = 'Declare @@config as NVARCHAR(MAX) 
UPDATE A SET A.[Value_Text] = (SELECT COALESCE(@@config + '', H'' + cast(A.[NEW] as nvarchar), ''H'' + cast(A.[NEW] as nvarchar)) 
FROM ##TEMPHCONVERT A)
FROM ' + @DestDatabase + '.[dbo].[DIM_CONFIG] A ON A.[Config_Desc] = ''JBCODE FTE Map Hierarchy  List'' ;' 
exec (@@sql);

答案 1 :(得分:0)

我不确定你为什么会收到这个错误,但这只是你所拥有的一点点改写。我不是连接一个长字符串,而是声明一个模板,然后在执行之前替换参数。它让我对它更具可读性。我将所有相关代码移动到@@ sql中,然后将结果返回到临时表中。

Declare @@sql as nvarchar(max);
Declare @SourceDatabase as nvarchar(10);
Declare @DestDatabase as nvarchar(10);
Declare @@Harray as nvarchar(max) ;

Set @SourceDatabase = 'D_EXP_CPM';
Set @DestDatabase = 'T_EXP_CPM';
set @@Harray = '1,2,3';

set @@sql = '
SELECT B.Hierarchy_SKey AS ''NEW'', A.Hierarchy_SKey AS ''OLD''
INTO #TEMPHCONVERT
FROM [[source]].[dbo].[DIM_HIERARCHY] A 
INNER JOIN [[dest]].[dbo].[DIM_HIERARCHY] B 
ON A.Hierarchy_Desc = B.Hierarchy_Desc
WHERE A.Hierarchy_SKey in ([Harray]);

Declare @@config as NVARCHAR(MAX);
SELECT @@config = COALESCE(@@config + '', H'' + cast(A.[NEW] as nvarchar), ''H'' + cast(A.[NEW] as nvarchar)) 
FROM #TEMPHCONVERT A;

select [config] = @@config into ##config
'

set @@sql = replace(@@sql, '[source]', @SourceDatabase)
set @@sql = replace(@@sql, '[dest]', @DestDatabase)
set @@sql = replace(@@sql, '[Harray]', @@Harray)

exec (@@sql);

select * from ##config

drop table ##config

答案 2 :(得分:0)

将@config声明为NVARCHAR(MAX);

SELECT @config = COALESCE(@config + ', H' + cast(A.[NEW] as nvarchar), 'H' + cast(A.[NEW] as nvarchar)) 
FROM #TEMPHCONVERT A;

set @sql = 'UPDATE A SET A.[Value_Text] = (@config )
FROM ' + @DestDatabase + '.[dbo].[DIM_CONFIG] A WHERE A.[Config_Desc] = ''JBCODE FTE Map Hierarchy  List'' ;'


PRINT 'STEP 15 COMPLETE';