last day (15 days later) » 

5:31 PM
0
Q: Create table from nth result set of stored procedure

Sava B.I have a stored procedure that returns 4 result sets. The results sets have lots of columns. What's the best way to create four tables out of each result set? The data types and schema in the tables should be the same as the ones from the result sets. I know I can do this to create a table fr...

 
Can you not do a select into? if you just get table objects back from the sproc you could probably do a select * into newtable from @returnedsproctable
 
I should have mentioned that I know next to no t-sql. I'm not sure how to select a single result set from a sproc.
 
how are they consumed currently?
 
It's a long story. Short answer is, I'm not sure. They're consumed by a foxpro report in some way. I should mention that the four tables I wish to create from result sets don't yet exist.
 
The into would create the table. If they existed already you'd need to do an insert.
 
5:31 PM
How do I get @returnedsproctable?
 
6:15 PM
 
yeah, i've got some code working that gets the results and splits them into DataSets, but I'd like to have run this in sql... I wanted to create the 4 tables by running some t-sql in microsoft sql management studio
 
The only way to access multiple results sets within the context of T-SQL is via SQLCLR.
 
6:28 PM
dont you have to modify, or rather.. redeploy the sproc/function if you want to use CLR?
how many times are you going to create these tables? meaning. is this a thing that needs to be repeated, or is it a one time deal
 
@paqogomez: are you asking if the current T-SQL proc has to be redeployed?
 
ya
 
nope
 
well thats cool, that might be the way to go, but there are downsides to sqlclr right? I've never used it in a production environment
 
The SQLCLR proc would simply exec the existing T-SQL proc and can either spit out a single result set (assuming an input param would specify which one to return as a result set) or could do a separate connection and directly call INSERT statements to do all 4 at the same time..
Same downsides to SQLCLR as with anything else.
 
6:32 PM
one time deal
 
seems over kill for a one time deal... just open the sproc, rip out the select statements individually and make them into a select into
 
Can be coded to be inefficient, but so can T-SQL. Can be coded to allow for accessing stuff that it shouldnt, but DELETE permissions for the wrong person can be devastating, and a DBA can dictate that the SQLCLR code cannot, under any circumstances, access the outside system (unlike using sp_OA* functions).
 
heh.. that's what I'm ending up doing. But if you saw this sproc... well, an overkill would be better
 
In light of this being a one-time operation, I agree with @paqogomez that the SQLCLR route is too much effort.
 
yep
I thought there would be a simple way
 
6:35 PM
very best of luck to you. Great info on clr @srutzky, thanks
 
It won't help now, but I will mention that I sell a library of SQLCLR functions (SQL# -- sqlsharp.com ) and in the next version i will have a Stored Proc that does all of these stuff and more.
 
haha ok, thanks man
 
and a plug! well, now all respect is gone... lol. totally kidding.. props :)
 
I mention it because it would make the time required to do this a matter of minutes, but with a small cost.
 
i like t-sql less and less every day. And i wasn't fond of it to begin with =P
but wouldn't you need to run c# code?
 
6:36 PM
@paqogomez, I only mentioned as it was specifically on topic.
 
you havent mentioned anything that isnt straight sql
it was.. i was just having fun at your expense :)
 
and the code to do it is much more than I could even paste into an answer here.
 
or is sqlclr a runtime that can interpret t-sql or something?
sorry, I'm new to all this
 
k ;-)...just clarifying...I try to not just plug my product as I know that is frowned upon here on S.O.,..
 
perfectly on topic. :)
 
6:38 PM
@SavaB. : SQLCLR is common name of the .Net integration in SQL Server..
it is how you can run, directly in the SQL context, VB.Net and/or C# and/or any .Net language, code.
 
ah i get it
 
ya, dont get down on t-sql just yet. I think you dont know enough yet. if you have issues pulling out each individual select, or turning them into select into's post another question.. should be super easy to resolve
 
So you would call a Stored proc, or function, or aggregate function, but the underlying code is in C# (or any .Net language) and not T-SQL..
 
right
 
and T-SQL is actually pretty nice and functional. It just has some limitations which make perfect sense as it was never intended to be a programming language.
 
6:41 PM
haha. I agree with everything, except the 'nice' part
 
again, it all depends on expectations.
if you are expecting a programming language, you will be sorely disappointed.
 
It's functional.. but it's painfully clear that it wasn't meant as a programming language. It's just nasty to read... I'm working with a product where they attempted to implement all business logic in t-sql
and it's just.. nasty
haha right, I agree. I'd use it for tasks where it would boost performance but.. the syntax and the length of the average sproc (thousands of lines from what I see)... well, I guess I should just learn it and ge tused to it lol
 
SQL is meant to manipulate data...and sometimes it is more practical and/or efficient to do some amount of logic while you are there, especially if there are multiple steps involved. But handling complex logic is not what it's for.
 
 
2 hours later…
9:02 PM
@paqogomez : if you are curious about SQL CLR (security concerns, capabilities, etc) then check out a series that I am writing on SQL Server Central: sqlservercentral.com/stairway/105855
 
9:22 PM
Thanks @srutzky , I will
 

last day (15 days later) »