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.
3条答案
按热度按时间wfsdck301#
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:
The code would be like this:
ecr0jaav2#
use Functions instead of Stored procedures in SELECT clause.
Edited:
create that function
then
Does this make sense?
4smxwvx53#
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. 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:
As mentioned previously, the most effective way to perform your query would be to perform that select inside a function, something similar to this I think will do:
You could then do exactly as you want...