Monday, April 25, 2011

SQL Server - Selectively inserting fields into temp table

I am executing a SP within a SP. The SP returns say 10 params. I am interested in only 5 of them. How do I insert only these 5 into the temp table.

The code I have so far:

  DECLARE @tmpUnion TABLE
  (
    UnionCode VARCHAR(10),
    UnionDate DATETIME,
    UnionPosition VARCHAR(30),
    UnionInitFees BIT,
    UnionDues BIT
  )

  --getDetails returns 10 params. I need only these 5
  INSERT INTO @tmpUnion
  (UnionCode, UnionDate, UnionPosition, UnionInitFees, UnionDues)
  EXEC getDetails
        @iUserId = @OriginalLoginId
From stackoverflow
  • You can't. The table variable must match exactly the structure of waht is being returned.

    Chris Lively : A better answer would show the way instead of just saying "you can't"
  • Put the result of getDetails into a tablevar that contains all of the return values, then do your insert off of the additional table.

    You might also check out this site for more information on how to share data between stored procedures.

    DotnetDude : I lied when I said the getDetails returns 10 values. Actually, it returns more than 50. If possible I want to avoid the additional overhead of creating a temp table with all these columns
  • Use OPENROWSET like so:

    Select 
           *
    from OPENROWSET('SQLOLEDB','Data Source=Server_name;Trusted_Connection=yes;
          Integrated Security=SSPI','Execute yourdb..get_orders')
    

    Now you can easily filter the resultset

    Select 
    employeeid,orderid,orderdate 
    from 
    
    OPENROWSET('SQLOLEDB','Data Source=Server_name;Trusted_Connection=yes;
           Integrated Security=SSPI','Execute yourdb..get_orders')
    
    where
           orderdate>='19960101' and orderdate<'19970101'
    

    You don't need to create a temp table and you also don't need to worry about the structure of the procedure.

    Found here

    EDIT: Final solution moved from comments after discussion.

    DotnetDude : #TempTable creates a global table right? When I declare this, I don't need to specify the columns and the datatypes?
    WakeUpScreaming : Double hash (##) creates global table. You will need to know the structure of the result set. I think I worked around this recently, I'll have to find the code.
    WakeUpScreaming : I am, of course, assuming that you don't have access to the stored procedure or for some other reason cannot modify it.
    DotnetDude : True, I cant modify the callee SP
    WakeUpScreaming : I found an awesome solution to your problem. I'm not sure it is the same thing I did a few months back - I still haven't found it. Look here: http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx
    WakeUpScreaming : 2nd solution, in case you are like me and reject it after reading the first few sentences.
    WakeUpScreaming : I'm a new user, so I don't yet know how to use this site to its fullest potential. The link I provided should probably be on the main page of this question instead of buried in the comments because it is almost certainly the best solution.
    DotnetDude : WakeUPScreaming - AFAIK, the OpenRowSet requires a connection string. This is an enterprise app that has the connection string tucked away in the config files.
    WakeUpScreaming : I think if the DBA gave you the name of the stored proc, they might give you a basic connection string. Worth a try.
    WakeUpScreaming : Check out the docs for OPENROWSET http://msdn.microsoft.com/en-us/library/ms190312(SQL.90).aspx Also consider that your stored procedure should be able to get the values needed for the parameters by querying its running context. I hope that makes sense.

0 comments:

Post a Comment