-1

これがストアドプロシージャの動的SQLです

BEGIN

SET NOCOUNT ON;
declare @SQL as varchar(4000)

set @SQL = 'CREATE VIEW vw_GCS_Export
    As
    select division, [primary image id],[Item Number], [Brand Name],[Marketing Description],
    [Colours]as Colors,[Live Date],[Sample Type],substring([FileName],charindex('+ CHAR(39) + '_' + CHAR(39) +',[FileName],1)+1,CHARINDEX('+ CHAR(39) + '.' + CHAR(39) +',[FileName],1)-5) as BatchID,
    '+ CHAR(39) + '670' + CHAR(39) +' as Status, [Primary image ID] + '+ CHAR(39) + '_P' + CHAR(39) +' as [Shot Name],
    Cast(Null as varchar(50))as [Swatch/Variation],Cast(Null as varchar(50))as [Alternate Shot],[UserName],''' + CAST(convert(date,Sysdatetime(),110)as varchar(10)) + ''' as [Import Date],
    Cast(Null as varchar(50))as [Return Instructions],Cast(Null as varchar(50))as [Rush Request], DATEADD(WEEKDAY,-5,[live date]) as [ImageDeliveryDate],
    '  + convert(varchar(50), ' + ' +  CHAR(39) + 'Division ' + CHAR(39) +' + [Division] + '+ CHAR(39) + ' * ' + CHAR(39) +' + [UserName] ) as JobName 
    from Sheet1$'


EXEC @SQL
select * from vw_GCS_Export

END

実行時にエラーが発生します

Incorrect syntax near the keyword 'EXEC'.

理由はわかりません。SQL ステートメント内にエラーはないようです。

4

3 に答える 3

5

括弧が必要です:

EXEC (@sql)

生成された SQL を変数から実行する方法を参照してください。

于 2013-04-01T19:04:06.427 に答える
0

これが私が最終的に得たものです:

SET NOCOUNT ON;
declare @SQL as varchar(4000)

set @SQL = 'CREATE VIEW vw_GCS_Export
    As
    select division, [primary image id],[Item Number], [Brand Name],[Marketing Description],
    [Colours]as Colors,[Live Date],[Sample Type],substring([FileName],charindex('+ CHAR(39) + '_' + CHAR(39) +',[FileName],1)+1,CHARINDEX('+ CHAR(39) + '.' + CHAR(39) +',[FileName],1)-5) as BatchID,
    '+ CHAR(39) + '670' + CHAR(39) +' as Status, [Primary image ID] + '+ CHAR(39) + '_P' + CHAR(39) +' as [Shot Name],
    Cast(Null as varchar(50))as [Swatch/Variation],Cast(Null as varchar(50))as [Alternate Shot],[UserName],''' + CAST(convert(date,Sysdatetime(),110)as varchar(10)) + ''' as [Import Date],
    Cast(Null as varchar(50))as [Return Instructions],Cast(Null as varchar(50))as [Rush Request], DATEADD(WEEKDAY,-5,[live date]) as [ImageDeliveryDate],
    ' + CHAR(39) + 'Division ' + CHAR(39) +'+ cast([Division] as varchar(50)) + '+ CHAR(39) + ' * ' + CHAR(39) + ' + [UserName] as JobName 
    from Sheet1$'


    EXEC (@SQL)
    select * from vw_GCS_Export

重要なのは、文字列全体にキャストを使用できなかったことです。浮動小数点フィールド [Division] にしかキャストできませんでした。私はすべての助けに感謝します FreshPrinceofSO

于 2013-04-01T21:13:54.920 に答える