Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EXEC inside SELECT statement in SQL Server

Tags:

sql

sql-server

I need to use exec inside of select clause. The query for the exec is created according to the columns of the table to on which select clause if used. What i want to do is something like following:

 SELECT distinct 
     MTMain.[TableName],
     MTMain.[TableFKey], 
     (select IsActive (exec GetStringForIsActive MTMain.[TableName],MTMain.[TableFKey]))
 FROM
     [MasterTableForLanguage] MTMain

Here, GetStringForIsActive is the stored procedure I want to execute for every row selected from MasterTableForLanguage.
The stored procedure will use EXEC to execute the following string

select IsActive from [TableName] where PKID= cast([TableFKey] as int)

TableName and TableFKey will be inputs of the stored procedure.

like image 387
Ruchit Rami Avatar asked Jun 27 '12 09:06

Ruchit Rami


People also ask

Is it possible to Exec a SELECT statement?

It's not EXEC'ing that statement, simply performing a select. I have several stored procs I use daily in some SQL agent processes, they all perform selects to query various tables, and none of them call an EXEC to perform those actions. This is my own example:

How to use SELECT query in SQL Server with exec?

SELECT is not possible with EXEC, You will have to get the EXEC output to a temp table or table variable and fetch the output from there Prior to sql 2012 SELECT is not possible. you need to get the result into a table (can be #table or tbl variable). But in 2012 you can do this..

What is EXEC SQL statement in SQL Server?

In this article, we will review on EXEC SQL statement in SQL Server and explore a few examples. The EXEC command is used to execute a stored procedure, or a SQL string passed to it. You can also use full command EXECUTE which is the same as EXEC.

Can a stored procedure Exec a SELECT statement?

Well, I think to answer the full question, I don't believe a stored procedure would EXEC a SELECT statement, it would simply perform the SELECT. You EXEC your current proc and pass it vars, and it returns a value BASED ON the select statement it runs. It's not EXEC'ing that statement, simply performing a select.


Video Answer


1 Answers

If you can modify your Stored Procedure GetStringForIsActive to return TableName, TableFKey and IsActive, you can use a cursor to get execute it for each row and add the results to a temp table.

ie:

exec GetStringForIsActive 'TableName', 'TableFKey'
returns
select [TableName], [TableFKey], IsActive from [TableName] where PKID= cast([TableFKey] as int)

The code would be like this:

declare @TableName nvarchar(50)
declare @TableFKey nvarchar(50)
declare @Results table (TableName nvarchar(50), TableFKey nvarchar(50), IsActive bit)

declare TableCursor cursor fast_forward for
   select TableName, TableFKey from MasterTableForLanguage

open TableCursor

fetch next from TableCursor into @TableName, @TableFKey

if @@FETCH_STATUS <> -1
   print 'MasterTableForLanguage check'

while (@@FETCH_STATUS <> -1)
begin


   insert into @Results
   exec GetStringForIsActive @TableName, @TableFKey

   fetch next from TableCursor into @TableName, @TableFKey

end
close TaleCursor
deallocate TableCursor

select * from @Results
like image 149
Lex Avatar answered Sep 20 '22 00:09

Lex