所有我有在這裏一個小問題是示例代碼SQL參數爲char []
create table Sections (ID int, name char(1), Description nvarchar(50))
insert into Sections values (1,'A','A Section')
insert into Sections values (2,'B','B Section')
insert into Sections values (3,'C','C Section')
insert into Sections values (4,'D','D Section')
insert into Sections values (5,'E','E Section')
insert into Sections values (6,'F','F Section')
select * from Sections
declare @Names nvarchar(10) ='A,B,C';
select *from Sections where name in (@Names)
我想要的是,我將傳遞一個字符串,字符串將被splited到每個字符和字符數組將在where子句中傳遞。 我試過它與數字,工作正常,但在字符它無法正常工作。 我所做的就是這樣
declare @Categories nvarchar(50)='ABC';
declare @array nvarchar(50);
declare @lenth int =len(@Categories);
Declare @c int =1;
declare @param nvarchar(50)='' ;
while @c <[email protected]
begin
set @param = @param + ''''+ SUBSTRING(@Categories ,@c,1) +''''+','
set @c [email protected]+1
end
print @param
declare @Cats nvarchar(50);
set @Cats= substring(@param, 0,len(@param)-0)
它的輸出,如 'A', 'B', 'C'。但是當我將這個@Cats傳遞給sp時,這將不會返回任何記錄。
還有一件事我不贊成將這個查詢作爲動態查詢來執行。
[您自己寫了哪個位?](http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-記錄/ 2837662#2837662) – 2011-02-02 18:28:04