-1
declare @sImport_Table1 table
(
Id int identity(1,1) ,
Zone nvarchar(50),
Sub_Code nvarchar(50),
Geography nvarchar(50),
DayOfWeek nvarchar(50)
)
INSERT INTO @sImport_Table1 SELECT 'A','Z','Geo','SUN'
declare @sZone nvarchar(50)
declare @sSubCode nvarchar(50)
declare @c_Geography nvarchar(50)='Geo'
declare @c_DayOfWeek nvarchar(50)='SUN'
declare @sImport_Table nvarchar(500)='@sImport_Table1'
--SELECT * FROM @sImport_Table1
declare @sQuery nvarchar(4000)
SET @sQuery='SELECT '+ @sZone + ' = Zone,'+
@sSubCode +' = Sub_Code
FROM'+ @sImport_Table +
' WHERE Geography ='+ @c_Geography +
' AND DayOfWeek = '+ @c_DayOfWeek
PRINT @sQuery
***************編輯******************** ************PRINT不打印動態sql
declare @sImport_Table1 table
(
Id int identity(1,1) ,
Zone nvarchar(50),
Sub_Code nvarchar(50),
Geography nvarchar(50),
DayOfWeek nvarchar(50)
)
INSERT INTO @sImport_Table1 SELECT 'A','Z','Geo','SUN'
declare @sZone nvarchar(50)
declare @sSubCode nvarchar(50)
declare @c_Geography nvarchar(50)='Geo'
declare @c_DayOfWeek nvarchar(50)='SUN'
declare @sImport_Table nvarchar(500)='@sImport_Table1'
--SELECT * FROM @sImport_Table1
declare @sQuery nvarchar(4000)
SET @sQuery='''SELECT @sZone = Zone, '+
'@sSubCode = Sub_Code
FROM '+ @sImport_Table +
' WHERE Geography ='''+ @c_Geography + '''
AND [DayOfWeek] = '''+ @c_DayOfWeek +''''''
PRINT @sQuery
sp_executesql @sQuery
PRINT @sZone
PRINT @sSubCode
實際上我想EXEC(@sQuery)並通過動態SQL執行在變量中獲取ZONE和SUB_Code的值。 – rahularyansharma
您不能使用'EXECUTE'來傳入IN/OUT參數。使用'sp_executesql'。 –
請檢查問題的編輯部分。 sp_executesql引發錯誤。 – rahularyansharma