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.
Related
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.
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.
I have a stored procedure that, ending with a SELECT, returns a recordset. I can call it within anoher stored procedure like this:
EXEC procedure #param
How to get the returning recordset? Thanks
You can create a temp table and then use INSERT INTO #MyTable EXEC procedure #param.
There are some other techniques listed here.
AFAIK, you can't. What you probably want to do is use a function for your first (or both) procedures. Functions can only return one thing, but they can return a table. Stored procedures can return multiple results, but not to other functions/stored procedures.
e.g.:
CREATE FUNCTION [dbo].[fn_GetSubordinates] (
#sPersonID VARCHAR(10),
#nLevels INT
)
RETURNS #tblSubordinates TABLE
(
Person_Id VARCHAR(10),
Surname char(25),
Firstname char(25)
)
AS
BEGIN
...
If you are using SQL Server 2008, I would recommend returning a Table-Valued Parameter.
http://msdn.microsoft.com/en-us/library/bb510489.aspx
You can do this with an output variable in the stored proc. For example:
CREATE PROCEDURE sp_HelloWorld #MyReturnValue int OUT
AS
SELECT #MyReturnValue = 100
Return #MyReturnValue
To call this stored proc, do the following:
DECLARE #TestReturnVal int
EXEC sp_HelloWorld #TestReturnVal output
SELECT #TestReturnVal
First, you CANNOT RETURN a recordset by stored procedure. By return, a stored procedure can only return integers.
You mentioned SELECT statement, which is a DQL and just for display purpose.
The way you can do to work around this issue is that you can assign the recordset to a global temporary table which can also be accessed within the outer stored procedure.
I would like to create a SP or UDF where I supply a table and column name as a parameter and it does something to that target. I'm using Sql Server 2005
Trivial Example of what I'm trying to accomplish:
CREATE FUNCTION Example (#TableName AS VARCHAR(100))
RETURNS TABLE
AS
BEGIN
SELECT *
INTO #temp
FROM #TableName
RETURN #temp
END
The example is just something trivial to illustrate what I'm trying to accomplish in terms of passing the Table name as a parameter.
Is this possible to do w/o concatinating strings and calling the EXEC function?
Ultimately, I'm trying to convert the answer from this question into something reusable.
This reeks of SQL injection. You would still need to use EXEC to do this.
No. Can't do it. Sadly, there is no macro pre-complier in T-SQL. The closest you'll get is SQLCMD mode, but that's only for scripts, can't use it in object definitions.
Are you doing the same thing to the table each time?
You could dynamically redefine a synonym, but that still requires an EXEC and you lose concurrency. You could serialize execution with a queue, but at that point you may be better off w/ plain old dynamic SQL.
You might try temporary tables, not passed in as a variable, but created in the parent connection or calling procedure. eg.
create proc #proc as
select * from #table
go
create table #table (col1 int)
insert #table values (1)
insert #table values (2)
insert #table values (3)
go
exec #proc
go
For more ways to share data between stored procedures, see here: http://www.sommarskog.se/share_data.html
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?