参数未传入SQL [英] Parameters did not passed in into SQL
本文介绍了参数未传入SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我有以下SQL语句,其中我想将参数传递给IN子句。参数可以具有多个或仅具有单个值。例如。测试或测试,测试2。但不知何故,参数没有像我想要的那样传递到IN子句中。以下是我的代码:
SELECT P.DocStatus
FROM vsCustomizationRequest.dbo.CRF_Project P,vsCustomizationRequest.dbo.CustomerList C
WHERE C. CustomerName IN ( SELECT CustomerName 来自 funcListToTableInt2( @ name ,' ,')) AND P.CustomerID = C.CustomerID
以下是我的功能:
创建 功能 [dbo]。[funcListToTableInt2]( @ list as varchar ( 8000 ) , @ delim as varchar ( 10 ))
RETURNS @ listTable table (
Value nvarchar (Max)
)
AS
BEGIN
- 声明帮助器以识别delim的位置
DECLARE @ DelimPosition INT
- < span class =code-comment>填充循环,初步检查delim
SET @ DelimPosition = CHARINDEX( @ delim , @ list )
- 循环,直到我们不再找到分隔符
WHILE @ DelimPosition > 0
BEGIN
- 将项目添加到表格
INSERT INTO @ listTable (值)
VALUES (CAST(RTRIM) ( LEFT ( @ list , @ DelimPosition - 1 )) AS nvarchar (最大)))
- 从列表中删除条目
SET @ list = right (< span class =code-sdkkeyword> @ list ,len( @ list ) - @DelimPosition )
- 执行职位比较
SET @ DelimPosition = CHARINDEX( @ delim , @ list )
END
< span class =code-comment> - 如果我们还有一个条目,请将其添加到列表中
IF len( @ list )> 0
insert into < span class =code-sdkkeyword> @ listTable (Value)
values (CAST(RTRIM( @list ) AS nvarchar (Max)))
返回
END
GO
解决方案
你会对TVP感兴趣吗?
SQL Server 2008用户定义的表类型和表值参数 [ ^ ]
只需更改
SELECT CustomerName 来自 funcListToTableInt2( @ name ,' ,')
进入
SELECT 值来自 funcListToTableInt2( @ name ,' ,跨度>)
Hi i have the following SQL statement in which i wanna pass the parameters into the IN clause. The parameters may hav multiple or just single value. Eg. Test or Test,Test2. But somehow the parameters did not passed into the IN clause as i want it to. Below are my codes:
SELECT P.DocStatus
FROM vsCustomizationRequest.dbo.CRF_Project P, vsCustomizationRequest.dbo.CustomerList C
WHERE C.CustomerName IN (SELECT CustomerName from funcListToTableInt2(@name, ',')) AND P.CustomerID = C.CustomerID
And below are my function:
CREATE FUNCTION [dbo].[funcListToTableInt2](@list as varchar(8000), @delim as varchar(10))
RETURNS @listTable table(
Value nvarchar(Max)
)
AS
BEGIN
--Declare helper to identify the position of the delim
DECLARE @DelimPosition INT
--Prime the loop, with an initial check for the delim
SET @DelimPosition = CHARINDEX(@delim, @list)
--Loop through, until we no longer find the delimiter
WHILE @DelimPosition > 0
BEGIN
--Add the item to the table
INSERT INTO @listTable(Value)
VALUES(CAST(RTRIM(LEFT(@list, @DelimPosition - 1)) AS nvarchar(Max)))
--Remove the entry from the List
SET @list = right(@list, len(@list) - @DelimPosition)
--Perform position comparison
SET @DelimPosition = CHARINDEX(@delim, @list)
END
--If we still have an entry, add it to the list
IF len(@list) > 0
insert into @listTable(Value)
values(CAST(RTRIM(@list) AS nvarchar(Max)))
RETURN
END
GO
解决方案
Would you be interested in a TVP ?
SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]
Simply change the
SELECT CustomerName from funcListToTableInt2(@name, ',')
Into
SELECT Value from funcListToTableInt2(@name, ',')
这篇关于参数未传入SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文