I want to create a SQL Server stored procedure with a varying number of parameters. It is similar to "params" in C#.
How can I do it?
Put them in an XML and try OPENXML feature.
http://msdn.microsoft.com/en-us/library/ms186918.aspx
You cannot.
What you can do is provide a default value for some of your stored procedure parameters, so you don't have to specify them when calling your stored procedure.
If you're on SQL Server 2008 or up, you could also investigate the table-valued parameter (or here) - basically the ability to pass in a table of data to your stored procedure. Maybe that'll help.
Related
Can I used Oracle Stored procedure as a data source for Spotfire ?
Yes, you can use stored procedure in spotfire.
All You need to do is create a procedure object and then information link that uses stored procedure.
Note: Make sure that procedure doesn't have any output parameters
Thanks,
P
I'm not super familiar with stored procedures in general and SQL Server/T-SQL specifically. I'm wondering if there is a way to alias or rename the columns returned by a stored procedure without modifying the stored procedure itself.
Here is the stored procedure call I have now.
EXEC sp_GetNearbyLocations 38.858907, -77.261358
It returns records with the following columns:
State
Zip
Phone
StartDate
Directions
Hours
Latitude
Longitude
Distance
However I'd like them to be all lowercase and not camel cased. Sadly, I do not control the stored procedure so I cannot change it, just my call to it. Is this possible in SQL Server 2008?
If SQL Server 2012 you can use the WITH RESULT SETS feature.
Otherwise this isn't possible it would require you to insert the results into some kind of intermediate temporary table then select from that.
(You could do this without creating the temp table explicitly by using OPEN ROWSET however)
You could write your own proc that simply calls the other one, aliases the columns and returns that.
On a side note - why does the case of the column names matter to you?
I have a stored Procedure that returns multiple resultsets(to be specific three resultsets). I just need the first resultset. I am calling the original procedure from a different procedure where i will store the returned resultset into a #table and use it from my further processing.
Also, i can not modify the original stored procedure to achieve this.
Please help!
It's not possible to retrieve the second or further result set from a stored procedure inside SQL.
Two workarounds:
A scheduled job (like a C# program) that periodically calls the stored procedure and stores the result in tables that other procedures can use.
A SQL CLR stored procedure that does the same. The advantage of a SQL CLR procedure is that you can call it from normal SQL stored procedures, so you don't have to wait for the scheduled task.
I haven't tested this, but a work around would be to use OpenQuery and call your SP using it because "Although the query may return multiple result sets, OPENQUERY returns only the first one". OPENROWSET will also do the same...
I have a stored procedure that returns a rowset that I'd like to pass into a CLR stored procedure to do some advanced calculations.
How would I set this up? Take the input? Iterate the rowset within the CLR procedure?
The best would be to have the CLR procedure execute the stored procedure itself, with an ordinary SqlCommand and iterate over the result as an ordinary SqlDataReader. This is the best way, since you avoid the extra copy of the result.
Another option would be to set up a SQLCLR Aggregate function. Depending on the structure of your formula, this might be a more natural syntax.
I have several stored procedures that all use the same set of parameters. Is there a way to define and save the parameter list as a reusable block of code? Something like this:
CREATE PROCEDURE test
Using StoredParameterList
AS
BEGIN
SQL Statement
END
Is this possible? It would make code maintenance easier if a parameter needed to be changed.
Well, sort of. I've never used them, but Sql Server supports something called User Defined Types. I suspect you can create a user-defined type that represents your parameter list and then just have one parameter on each procedure with UDT.