SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to call dynamic query stored procedure in select statement


how to call dynamic query stored procedure in select statement

Author
Message
shah_khan_1289
shah_khan_1289
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 8
Hello,

i have created a stored procedure with dynamic query and using sp_executesql . stored procedure is work fine.

now i want to call stored procedure in select statement because stored procedure return a single value.

i search on google and i find openrowset but this generate a meta data error

so how i can resolve it

please reply fast
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41402 Visits: 19506
shah_khan_1289 (7/23/2014)
Hello,

i have created a stored procedure with dynamic query and using sp_executesql . stored procedure is work fine.

now i want to call stored procedure in select statement because stored procedure return a single value.

i search on google and i find openrowset but this generate a meta data error

so how i can resolve it

please reply fast


Quick thought, use exec into a temp table/table variable and select from there.
Cool
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67757 Visits: 18570
Can you provide more detailed information with sample DML?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

shah_khan_1289
shah_khan_1289
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 8
Eirikur Eiriksson thanx for quick reply.


can you please provide me example.
shah_khan_1289
shah_khan_1289
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 8
this is my first sp

create proc createSp1(@acc varchar(20),@columnname varchar(25))
as

declare @value flaot;
set @query='select top 1 @value='+@columnname+' distict where DISTRICT='+@District;
-- select @query;
EXEC sp_executesql @query,N'@value nvarchar OUTPUT',@value OUTPUT;

select @value +100;
end
//---------------
this is my second sp
create proc sp2(@data varchar(20))
set @data='select distinct CURRENT_YR_RATE,exec sp1(acc,j.columname) as estimatedrate from dist j
where DISTRICT IN ('''+@data+''')';
EXEC sp_executesql @data
end

this is my DML
GilaMonster
GilaMonster
SSC Guru
SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)

Group: General Forum Members
Points: 229174 Visits: 46344
In short, you can't do that.

You'll need to either run the proc within a cursor, store the results into a temp table (insert into <table name> exec <proc>Wink or identify what the statements within the proc are and use those within the select (depending what they are)

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96739 Visits: 38988
shah_khan_1289 (7/23/2014)
this is my first sp

create proc createSp1(@acc varchar(20),@columnname varchar(25))
as

declare @value flaot;
set @query='select top 1 @value='+@columnname+' distict where DISTRICT='+@District;
-- select @query;
EXEC sp_executesql @query,N'@value nvarchar OUTPUT',@value OUTPUT;

select @value +100;
end
//---------------
this is my second sp
create proc sp2(@data varchar(20))
set @data='select distinct CURRENT_YR_RATE,exec sp1(acc,j.columname) as estimatedrate from dist j
where DISTRICT IN ('''+@data+''')';
EXEC sp_executesql @data
end

this is my DML


It may just be me but are you sure that the first stored procedure actually works?? I don't even see a FROM clause in the dynamic SQL, so where is it pulling data? Also, not too sure about your call to sp_executesql as it doesn't fully resemble anything I have written.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41402 Visits: 19506
Slightly puzzled how you got those procedure to work, are you certain that you posted the right code? Quickly looking at the first procedure:
1. Typo, flaot should be FLOAT, this will never work.
2. Missing variable declaration for the @query variable, will error.
3. 'distinct' directive in a wrong place, this will never work.
4. Missing FROM clause, this will never work.
No need to go any further until this is fixed. My suggestion to you is to describe as accurately WHAT you are trying to achieve and we will help you with HOW to do it.
Cool
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search