SQL Multiple Parameter Values - sql-server-2005

What options are available in SQL 2005 to pass in multiple values to store procedures
Psuedo codes
In C# code
List<string> myApplicationList;
.... (code to assign values)
**Construct parameter list**
Call stored procedure [spSelectMaster] with myApplicationList
SQL stored procedure
CREATE PROCEDURE [Ecn].[spSelectMaster]
**Need to declare parameter here but not sure what this would be**
AS
BEGIN
SET NOCOUNT ON
SELECT *
FROM [dbo].[Master]
WHERE [Master].[ApplicationKey] IN (#ApplicationList)
END
GO
Thanks in advance

There is no built-in support for arrays in SQL Server 2005 T-SQL, but you can work around this:
Arrays and Lists in SQL Server 2005
How to pass a list of values or array to SQL Server stored procedure?

Related

Using results of one stored procedure in another stored procedure - SQL Server

Is there any way to use the results of one stored procedure in another stored procedure without using a table variable or temp table? The logic is as follows:
IF (#example = 1)
BEGIN
DECLARE #temp TABLE (Id INT);
INSERT INTO #temp
EXEC [Procedure1] #ItemId = #StockId
set #Cost = (select top 1 id from #temp)
Ideally i would like to know if there is a way to do this without having to use a temp table. Looked around online but can't find anything that works. Any advice would be great.
In general, if you want to use user-defined code in a SELECT, then it is better to phrase the code as a user-defined function rather than a user-defined procedure.
That is, procedures should be used for their side effects and functions should be used for their return values.
That said, you can use openquery (documented here) to run an exec on a linked server. The linked server can be the server you are running on.

Creating a table type param in SQL Server Stored Procedure

Is it possible to declare/create the table type param(TVP) in Stored Procedure itself instead of creating the table value type separately in the schema and then using it in Stored procedure. i.e.,
create procedure proc1(
#table1 table(id int) readonly
)
as
begin
select top 1 * from sysobjects
end
From MSDN:
Table-valued parameters are based on strongly-typed table structures
that are defined by using Transact-SQL CREATE TYPE statements. You
have to create a table type and define the structure in SQL Server
before you can use table-valued parameters in your client
applications.
So it is clearly said that you can't declare TVP in stored procedure like you want - only by creating as user-defined type.

Is it possible to retrieve Selected Columns from Stored Procedure?

I have a stored procedure which returns a few columns from a SELECT. Now I need to grab 2 columns out of those columns in my new stored procedure and use them.. I am trying to do this using EXEC method. Is it possible to do this?
Ex : Original stored procedure:
CREATE PROCEDURE myBaseProcedure
#stId INT
AS
BEGIN
SELECT Name,
Address,
StudentId,
Grade
FROM Student
WHERE StudentId = #stId
END
New stored procedure:
CREATE PROCEDURE myNextProcedure
BEGIN
EXEC myBaseProcedure 19 -- Here I need to grab only StudentId and Name??
END
Given that you cannot dump to a temp table or table variable since the base stored procedure might sometimes add columns, there are three approaches that would do this:
You can effectively SELECT from a stored procedure using either OPENROWSET or OPENQUERY
You can use SQLCLR to create a table-valued function that executes the procedure, returns a struct of just the fields that you want, which will be the only fields that you read or "get" from the SqlDataReader.
You can use SQLCLR to create a stored procedure that executes the procedure to get a SqlDataReader, and instead of returning the SqlDataReader to SqlContext.Pipe.Send(), you would use SendResultsStart, SendResultsRow, and SendResultsEnd. You would create a SqlDataRecord of just the fields you wanted, and those would also be the only fields that you read or "get" from the SqlDataReader. While this still leaves you with a stored procedure, the filtering of the fields is done within the CLR-based proc so the output is guaranteed to be just the fields you want, regardless of how the result set structure of the base stored procedure changes. In this way you could create a local temp table to dump the results to, which would be better for JOINing to other tables. This method also allows for you to pass in a list of fields to the CLR-based stored procedure that would be parsed and used as the fields to dynamically construct the SqlDataRecord with as well as to dynamically determine which fields to get from the SqlDataReader. That would be a little more complicated but also quite a bit more flexible :).
You don't need to create a new stored procedure for this, you can integrate the stored proc call in a simple query using OpenQuery or use a temporary table.
Using OPENQUERY
SELECT Name,
Address
FROM OPENQUERY(ServerName, 'EXEC myBaseProcedure 19')
-- WHERE your_field = expected_value --> if you need to add filters
Using Temp table
Declare #MyTempTable Table (columns definitions)
Insert #MyTempTable Exec myBaseProcedure 19
Select Name,
Address
FROM #MyTempTable

Use Dynamic Query in a Procedure and call that procedure in a select statement in SQL Server

I need to execute a dynamic query in SQL Server and show the result in a select query.
Since, we cannot use dynamic queries in functions, we have written a stored procedure to return the dynamic query value.
But how can i call my stored procedure in a select query.
You normally would use the key word EXEC or EXECUTE to call a stored procedure.
yet there is a way of selecting data from a stored procedure using OPENQUERY.
SELECT * FROM
OPENQUERY(SERVERNAME, 'EXECUTE Proc_Name #parameters')

Use Stored procedure like a function

I need to deal with the table name as a variable.Then I must using dynamic sql and therefore I must using Stored procedure.
But the problem that how can I use the stored procedure like a custom sql function.
e.g: select col1,(Exec sp1 param1,'tbName') from table1
Finally,I changed my design and and use dynamic SQL in one upper level.
This will be posible in sql server denali that introduces the new keywords "WITH RESULTSET".
The alternative on current sql versions is passing a temp-table to the stored procedure
Stored procedures can return scalar values through output parameters. Here's an example (from here).
Create the stored procedure like this:
CREATE PROCEDURE _4P_test
#intInput INT,
#intOutput INT OUTPUT
AS
SET #intOutput = #intInput + 1
Call it like this:
DECLARE #intResult INT
EXEC _4P_test 3, #intResult OUT
SELECT #intResult
However you should try to design your system so that you don't have to use dynamic SQL in the way you described.