• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1459
  • Last Modified:

output parameter with sp_executesql

Hi Experts,

Is it possible to store the results of a call to exec sp_executesql in a parameter.  The parameter is the output for the stored procedure that calls the sp_executesql.

Something like this :-

@result nvarchar(1000),
@nSql nvarchar(100)

set @nSql = 'select * from employees where employeeID = @EmpID' --returns a single record
--@EmpID is a passed in value
set @result = exec sp_executesql @nsql
 --@result is an output parameter for this stored procedure

Regards,

Pete
                        
0
Pete_Burke
Asked:
Pete_Burke
  • 2
  • 2
  • 2
  • +2
1 Solution
 
adatheladCommented:
Hi,
No, you can't store a result set in a variable like this. What is it your trying to do? Do you have another stored procedure that needs to use the results returned from this stored proc for something? If so, you can load the results into a temp table within that 2nd stored proc and use the values from there:
CREATE PROC prNewProc
AS
CREATE TABLE #results
(EmployeeId INTEGER,
Forename VARCHAR(50),
.. etc
)

INSERT #Results
EXECUTE prGetEmployeeStoredProc 1

-- Now results are in #Results table for use
...
...
DROP TABLE #Results
GO
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this is the way you can get info back:
@result nvarchar(1000),
@nSql nvarchar(100)

set @nSql = 'select  @res = somefield from employees where employeeID = @EmpID' --returns a single record
exec sp_executesql @nsql, N'@EmpID int, @res nvarchar(1000) OUTPUT', 30, @result OUTPUT



I guess that you are aware that:
select  @result = somefield from employees where employeeID = @EmpID

will work easier?
0
 
Pete_BurkeAuthor Commented:
I need to select all the columns and I don't want to type in all the cloumn names as I have to do this for 100+ tables hence the 'select * from employees where employeeID = @EmpID'
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
assystCommented:
try this
---------------------------------------

@result nvarchar(1000),
@nSql nvarchar(100)

set @nSql = 'select * from employees where employeeID =' + @EmpID      --returns a single record
--@EmpID is a passed in value
set @result = exec (@nsql)
0
 
assystCommented:
@result nvarchar(1000),
@nSql nvarchar(100)

set @nSql = 'select * from employees where employeeID =' + @EmpID      --returns a single record
--@EmpID is a passed in value

exec (@nsql)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>I need to select all the columns and I don't want to type in all the cloumn names as I have to do this for 100+ tables hence the 'select * from employees where employeeID = @EmpID'
do you want to have all the column names into 1 variable as long string?
then you have NO CHOICE but do a

select field1 + field2 + field3  ... from yourtable

and even for each field that is not of data type varchar you need to convert it to varchar explicitely
also, you probably want a column separator.

if you want to have each column into 1 variable each, again you have NO CHOICE
0
 
adatheladCommented:
What do you need to do with the data it returns?

Somewhere along the line, you will probably need all the column names.
If it's the typing in of those column names your concerned with and the time associated with doing that, you can run a simple query to produce the list of column names for you (see below) and then just copy/paste the list into your SP.

DECLARE @Cols VARCHAR(8000)
SET Cols = ''

SELECT @Cols = @Cols + COLUMN_NAME + ','
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName'

IF (RIGHT(@Cols, 1) = ',')
    SET @Cols = LEFT(@Cols, LEN(@Cols) - 1)

PRINT @Cols


0
 
Pete_BurkeAuthor Commented:
Hi Experts,

Some back ground information, what I have to do is check about 100 tables in sql server to see if there have been any new records added after a specific date and time. Any changes detected must be reported to a front end GUI written in C#, the records will be displayed in a datagrid and later exported to XML.  Each table has a create date time column (Create_DTTM) So I need to loop through all the 100 table names and check if they have any records created after a certain date and time. If they do then select all of the record (select * from table) together with the column names and add it to some kind of "list", ideally inside sql server to aide performance rather than in the GUI application.  Once all of the tables have been checked display all the records in the GUI and then export to XML.  Any ideas on how to do this efficiently and dynamically using stored procedures or some other sql server method? Any suggestions on some kind of list and views on using a datagrid or some thing else. And any top tips on XML would be appreciated.

Regards,

Pete
0
 
rw3adminCommented:
this is not a solution, but should give you an idea of how to do this, my query is just doing a select you can change this accordingly, also change 'MyDate' to your desired date but make sure its in quotes.

Select       IDENTITY(int, 1,1) id,
      'Select * from '+sysobjects.Name+' where '+sysColumns.name+'=MyDate' Query
Into #T
from       sysColumns
Inner Join
      sysobjects
ON      sysobjects.ID= sysColumns.ID
where       sysColumns.name='ChangeDate'

Declare @Id int, @Query Varchar(8000)
Select @ID=1
While @ID<=(Select Max(ID) from #T)
Begin
Select      @Query =Query from #T where Id=@ID
Exec (@Query)
Select @Id=@Id+1      
End

0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now