我正在尝试编写动态 SQL 查询 - 尝试在参数中传递列名。我面临的问题是,当我尝试Convert
在字符串参数中传递 SQL 函数时,SQL Server 会引发语法错误。没有函数,Query 执行得非常好。错误是:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.
没有点“。” 我可以在我的查询中看到。但是,是的,作为参数传递给的数值Convert
是浮点值。有人可以告诉我我做错了什么吗?
非常感谢。
declare
@orderBy nvarchar(200),
@sortOrder nvarchar(40),
@fieldlist nvarchar(max),
@STMT nvarchar(max)
set @orderBy = 'Symbol'
set @sortOrder = 'Asc'
SET @fieldlist = 'Symbol, [Net Qty], [Total BUY], [Avg. BUY RATE], [Trans. Amount], [MTM Price], [MTM Amount], [Total SELL], [Avg. SELL RATE], [PENDING BUY], [PENDING SELL], Convert(numeric(10,2),[SETTLED P/L]) as [SETTLED P/L],
Convert(numeric(10,2),[UNSETTLED P/L]) as [UNSETTLED P/L] '
SET @STMT = 'SELECT ' + @fieldlist +
'FROM (
SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderBy + ' '+@sortOrder+' '+') AS row, *
FROM ' + '[Cats(Main)].[dbo].SESSION_HOLDING' + '
WHERE ' + 'AccountNo = '''+'00'+'''' + '
) AS tbl
WHERE
row > ' + CONVERT(varchar(9), @lbound) + ' AND
row < ' + CONVERT(varchar(9), @ubound)'
EXEC (@STMT)