错误“关键字'CONVERT'附近的语法不正确”

时间:2018-12-07 13:43:36

标签: sql-server tsql dynamic-sql

我有以下T SQL语句,但始终收到错误

  

关键字“ CONVERT”附近的语法错误

我要去哪里错了?

DECLARE @NSQL2 NVARCHAR(MAX)
DECLARE @SOURCETABLE NVARCHAR(MAX)

SELECT TOP 1 @SOURCETABLE = TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME LIKE 'IN_PRODUCT________________%' 
  AND TABLE_TYPE = 'BASE TABLE'

SET @NSQL2 = 'UPDATE [' + @SOURCETABLE + '] SET OnSale = '''+ CONVERT(nvarchar(MAX),OnSale, 112)+'''

EXEC SP_EXECUTESQL @NSQL2

3 个答案:

答案 0 :(得分:0)

尝试一下:

    SET @NSQL2 = 'UPDATE ' + QUOTENAME(@SOURCETABLE) 
                 + ' SET OnSale = CONVERT(nvarchar(10),OnSale, 112)'

答案 1 :(得分:0)

我通过以下方法使它起作用:

SET @NSQL = 'UPDATE [' + @SOURCETABLE + '] SET OnSale =  CONVERT(nvarchar,CAST(OnSale AS DATETIME), 120)'

答案 2 :(得分:-1)

您是否打算将Convert语句作为文字包含在字符串中?

SET @NSQL2 = 'UPDATE [' + @SOURCETABLE + '] SET OnSale = CONVERT(nvarchar(MAX),OnSale, 112)'