views:

65

answers:

2

Why did they never let us do this sort of thing:

Create Proc RunParameterisedSelect

@tableName   varchar(100),
@columnName  varchar(100),
@value       varchar(100)

as

select * from @tableName
where @columnName = @value

You can use @value as a parameter, obviously, and you can achieve the whole thing with dynamic SQL, but creating it is invariably a pain.

So why didn't they make it part of the language in some way, rather than forcing you to EXEC(@sql)?

A: 

Because procedures precompile on first execution into query plans, and this would not be possible like that. I agree it is paifull, it is only so, though,f or people which are stored procedure addicts - do not use sp's for queriey, build dynamic query strings and you have no problems.

TomTom
I'm not asking for cached query plans as well, any more than I would when building a string dynamically to EXEC. But this is no reason to force awkward and error-prone dynamic string-building on the programmer.
ChrisA
SQL does not assume dynamic execution like this. This is the idea. The concept is that you do not use stored procedures to make queries. THat simple. Then this is not needed. And I have not done string building in years - use a sql generator.
TomTom
+4  A: 

Quite simply because such a thing is impossible. This is a bit like asking, "Why was this JavaScript syntax never implemented":

var operator  = "<";
var statement = "if"
var op1       = 4

statement (op1 operator 5) op1++;  // whatever

It's never been implemented because this is un-implementable and, frankly, it does not make any sense. JavaScript has eval() for dynamic code:

code = statement+" (op1 "+operator+" 5) op1++;";
eval( code );

And SQL Server has EXECUTE for dynamic SQL:

/* example only, it is not recommendable to actually *do* this */
Create Proc RunParameterisedSelect
  @tableName   varchar(100),
  @columnName  varchar(100),
  @value       varchar(100)
as
begin 
  declare @code varchar(8000)

  set @code = 'select * from ['+@tableName+'] where ['+@columnName+'] = '+@value

  exec (@code)
end

The essence is the same - if it is not a fixed, immutable code structure (and a table or column name is code in SQL, not a variable), then you must make a string out out it first and parse that. The interpreter/compiler must build a new syntax tree (which itself will be fixed again, of course).

Tomalak
Well, firstly, I'm well aware of the Exec(@code) construct for dynamic SQL, as you'll see from my original question.Secondly, the T-SQL interpreter is already well able to parse a T-SQL statement with a parameter, and substitute the value of that parameter at run time. So the notion that such a syntax is unimplementable seems bizarre to me. If I can put a string together that effectively does the same job, then clearly a parser can be written to achieve the same thing without resorting to the horrors of dynamic SQL string manipulation....
ChrisA
@Chris: You are missing a fundamental point, one which I tried to make clear with the JavaScript example, and in the last paragraph. The table name is part of your *code*, it's not a variable. Just like the `SELECT` keyword is part of your code. The query is parsed and bound to your schema in the process; the table name or any other structural part of the query *cannot be variable*. This is the same misconception that leads people to ask "Why can't I do `WHERE x IN (@list)` or `SELECT @list FROM x`"?" -- Simple: because that's just not how it works.
Tomalak