日期:2014-05-18  浏览次数:20432 次

存储过程里的 WHERE EmployeeID in (@id) 如何传参数?
USE [TAMS]
GO
/****** Object: StoredProcedure [dbo].[get_EmployeeID] Script Date: 04/11/2012 14:30:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[get_EmployeeID] 
(
@id varchar(1024)
)
as
BEGIN
declare @sql nvarchar(2048)
SELECT * FROM Employee WHERE EmployeeID in (@id)
exec sp_executesql @sql
end



存储过程是这样写的,没有错,现在@id参数怎么传,
EXEC get_EmployeeID '001','002'
EXEC get_EmployeeID '001,002'
这样查询不出来
001,002也不行
只能单独 001,或者单独002才可以查询出来,怎么办?

------解决方案--------------------
SQL code

create proc [dbo].[get_EmployeeID]  
(
@id varchar(1024)
)
as
begin
declare @sql nvarchar(2048)
set @sql='SELECT * FROM Employee WHERE EmployeeID in ('+@id+')'
print @sql
end
go

exec get_EmployeeID '''001'',''002'''

drop proc get_EmployeeID

/*************************

SELECT * FROM Employee WHERE EmployeeID in ('001','002')