enigmasolutions
asked on
Stored Procedure with CSV or Table Variable Parameter in SQL 2005
It seems that SQL 2005 does not support Table Variable Parameters in Stored Procedures.
See this article...
http://weblogs.sqlteam.com/jeffs/archive/2007/06/26/passing-an-array-or-table-parameter-to-a-stored-procedure.aspx
This would have been my preference (but alas, I have some SQL 2005 customers). And I guess I am not thrilled about the idea of a parameter table as sugested in this article.
Anyway, in this particular application I have a comma separated list of stock numbers (all integers) that I want to pass in to a stored procedure and then used them to join in a query in the stored proc. The number of stock numbers is not likely to exceed 20. Calling the proc 20 times is not an option since it is a heavy duty task.
I want something like this:
create procedure MyProc @StockNos varchar(5000)
as
begin
Select * from myStockTable
where StockNo in (@StockNos)
end
but of course this doesn't work when you put a comma in @StockNos, you get error "Conversion failed when converting '48500,48482' to data type int."
Any ideas?
See this article...
http://weblogs.sqlteam.com/jeffs/archive/2007/06/26/passing-an-array-or-table-parameter-to-a-stored-procedure.aspx
This would have been my preference (but alas, I have some SQL 2005 customers). And I guess I am not thrilled about the idea of a parameter table as sugested in this article.
Anyway, in this particular application I have a comma separated list of stock numbers (all integers) that I want to pass in to a stored procedure and then used them to join in a query in the stored proc. The number of stock numbers is not likely to exceed 20. Calling the proc 20 times is not an option since it is a heavy duty task.
I want something like this:
create procedure MyProc @StockNos varchar(5000)
as
begin
Select * from myStockTable
where StockNo in (@StockNos)
end
but of course this doesn't work when you put a comma in @StockNos, you get error "Conversion failed when converting '48500,48482' to data type int."
Any ideas?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
wdosanjos, Yes I saw a link for that method here.
http://itworksonmymachine.wordpress.com/2008/08/03/table-valued-parameter-in-sql-server-2005/
So far we have two possible solutions. A third one would be to parse through the CSV string end insert values into an internal table variable. But that is ugly and not as efficient as the other two.
Anyone got any other ideas? (I doubt it).
For my record... here are two links on Table Value Parameters in 2008:
http://www.codeproject.com/Articles/37174/Table-Value-Parameter-in-SQL-Server-2008
http://www.techrepublic.com/blog/datacenter/passing-table-valued-parameters-in-sql-server-2008/168
http://itworksonmymachine.wordpress.com/2008/08/03/table-valued-parameter-in-sql-server-2005/
So far we have two possible solutions. A third one would be to parse through the CSV string end insert values into an internal table variable. But that is ugly and not as efficient as the other two.
Anyone got any other ideas? (I doubt it).
For my record... here are two links on Table Value Parameters in 2008:
http://www.codeproject.com/Articles/37174/Table-Value-Parameter-in-SQL-Server-2008
http://www.techrepublic.com/blog/datacenter/passing-table-valued-parameters-in-sql-server-2008/168
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well how many ways can you write an SQL statement? Answer = lots!
I will leave this open for a day just in case there are any others.
But, the answers so far will server my purposes.
Thank you.
I will leave this open for a day just in case there are any others.
But, the answers so far will server my purposes.
Thank you.
ASKER
All good solutions
ASKER
Only problem is my Stored Proc does a lot more than just Select * from MyStockTable.
It has while loops and other stuff (140 lines of SQL). So I would have to put the whole lot in @SQL. Which I would prefer not to do (results may be unpredicatable).