I have a stored procedure which need to run IN statement. I want to know a way to convert a string (list of comma separated integers) to int. In this example, positionID needs to be converted. Please help. Thanks
Here is my stored procedure:
Create PROCEDURE [dbo].[spCount]
@year varchar(50),
@positionID varchar(50)
AS
BEGIN
Select
ApplicantID, [Name], PositionID, COUNT(*) AS Votes
FROM dbo.vwLog
WHERE Year = @year And PositionID in (@positionID)
GROUP BY ApplicantID, [Name], PositionID
Order By PositionID, Votes DESC
END
You can take advantage of the fact that SQL Server 2008 now supports table types. You can define a table type and on the .net side construct a
DataTableand pass that as a parameter to your stored procedure. On the SP side that parameter is of type [whatever tabletype you made] Here is an example.You can then Append Positions as a parameter for your stored procedure
In your database you have to define a table type as
and in your stored procedure add
Now you can treat
@MyPositionsas a table in your procedure and compare to it.