0

I have an SQL stored procedure with several parameters, and I want to call it using several columns from (the same) row for some of the parameters. Here's what the current implementation looks like (not including the unused columns / parameters, which exist):

DECLARE @id int;
select @id = min( id ) from Table_1;
declare @param1 nvarchar(10), @param2 nvarchar(10);
select  @param1 = param1, @param2 = param2 from Table_1 WHERE id=@id;
EXEC [dbo].otherProc @param1, @param2;

This is an improvement over multiple selects, but I can't help wondering if there's a way of doing this without variables (except @id). Selecting directly in the EXEC statement doesn't seem to work (or maybe I don't know the right syntax for it).

7
  • 3
    You want table valued types/parameters.msdn.microsoft.com/en-us/library/… Commented May 18, 2016 at 13:05
  • You could change your procedures to accept just the @id parameter and perform your selects inside the procedure along with your other code.Oracle allows for a RowType which lets you specify a single variable/parameter as a row of a particular table, but afaik, SQL Server does not have this. Commented May 18, 2016 at 13:09
  • @gmiley for normal stored proc that might be a good idea, but in my case otherProc is CLR (autogenerated from C# code) so even if that worked it would only make the code worse. Commented May 18, 2016 at 14:08
  • @Giorgi Nakeuri I haven't worked with table-valued parameters, would it involve changing the parameters in the stored proc definition? In my case otherProc is CLR and from what I've seen, passing table-valued parameters to CLR is either impossible or extremely complicated. Commented May 18, 2016 at 14:12
  • Yes it requires changes to stored proc parameters. Anyway you can pass to proc only constants and variables. You can not have expressions there. Commented May 18, 2016 at 14:17

2 Answers 2

1

You could execute a dynamic solution:

DECLARE @sql nvarchar(max) 
SELECT @sql = '
EXECUTE dbo.OtherProc ''' + param1 + ''',''' + param2 + ''';'
FROM Table_1
WHERE id=@id;

EXECUTE (@sql);
Sign up to request clarification or add additional context in comments.

Comments

0

Unless you can change the stored procedure, the only improvement I came up with is this:

DECLARE @id int, @param1 nvarchar(10), @param2 nvarchar(10);
select @id = min( id ) from Table_1;

select  TOP 1 @id = id, 
        @param1 = param1, 
        @param2 = param2 
from Table_1 
order by id;

this way you populate all parameters in a single select statement.

2 Comments

My code also populates them all in a single statement. I was hoping to avoid parameters, since I don't do anything with them except pass values through them.
The code you posted populates the @Id first, and then the other parameters. mine populates the @id with all the others. again, if you can change the procedure, you can pass only the @id and populate everything else inside the procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.