6

I'm using SQL Server 2008 R2 and was wondering if there is another way of writing something like

EXEC dbo.myProcedure (SELECT columnName FROM TableName)

or

EXEC dbo.myProcedure @myStringVariable + 'other text'

so that these procedure calls actually work, without putting the whole stuff into a variable first.

Kara
  • 6,115
  • 16
  • 50
  • 57
cdbeelala89
  • 2,066
  • 3
  • 28
  • 39
  • possible duplicate of [Cast integer and concatenate to varchar in TSQL](http://stackoverflow.com/questions/4936180/cast-integer-and-concatenate-to-varchar-in-tsql) – Martin Smith Aug 19 '12 at 13:15
  • 6
    No it isn't possible. The stored procedure parameter list can only include literals, variables, parameters, and `@@` system functions. [Vote for this here](https://connect.microsoft.com/SQLServer/feedback/details/352110/t-sql-use-scalar-functions-as-stored-procedure-parameters) – Martin Smith Aug 19 '12 at 13:16

2 Answers2

1

Concatenation in inline execution of SP is not allowed..

Assuming that your @myStringVariable compromises of certain text and certain dynamic text you want to concatenate it.

Why don't compute it and assign to @myStringVariable before making the call to proc.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
mvm_bgm
  • 153
  • 1
  • 11
0

Not sure what you mean by "without putting the whole stuff into a variable first". However, I have a solution, even though it may go against what I think you mean. But here it is anyway.

DECLARE @SQL VARCHAR(MAX)

SET @SQL = ''

SELECT
    @SQL = @SQL + 'EXEC SprocName ''' + ColumnName + ''''
FROM
    MyTable 

EXEC (@SQL)

I use this method all the time, when I need to execute a sproc based on specific values from a table.

CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108