【发布时间】:2016-03-13 10:34:26
【问题描述】:
我有一个动态 sql。
declare @CustomerId int
set @CustomerId = 1
declare @SDate datetime
set @SDate = '2015/12/07'
declare @ItemId int
set @ItemId = 2
declare @QtyS nvarchar(max)
declare @QtyOut decimal(18,3)
set @QtyS = 'SELECT isnull(sum(d.Qty),0)
FROM InvoiceDetail AS d INNER JOIN
InvoiceHeader AS h ON d.InvoiceNo = h.InvoiceNo
where
h.CustomerId = '''+@CustomerId+''' and
d.itemmasterid = '''+@ItemId+''' and
h.Deleted = 0 and
h.invoicedate = '''+@SDate+''''
exec sp_executesql @QtyS, N'@Qty decimal(18,3) out', @QtyOut out
select @QtyOut
执行此附加错误时。 错误作为文本也附于此。 有什么想法吗?
Error:
Msg 245, Level 16, State 1, Procedure rpt_SpecialLaunch, Line 76
Conversion failed when converting the varchar value 'SELECT isnull(sum(d.Qty),0)
FROM InvoiceDetail AS d INNER JOIN
InvoiceHeader AS h ON d.InvoiceNo = h.InvoiceNo
where
h.CustomerId = '' to data type int.
【问题讨论】:
-
请将错误添加为文本
-
在连接之前将非字符串变量转换为字符串:
'xxx' + cast(@CustomerId as varchar(16)) + ' xxx'- 你所拥有的可以在线执行。
标签: sql sql-server sql-server-2005 dynamic-sql