Basically, I'm looking for an equivalent to SqlCommandBuilder.DeriveParameters
that will work for arbitrary T-SQL.
For example, this query requires one parameter:
SELECT @Foo [Foo], '@Bar' [Bar], @Baz [Baz]
I basically need to extract:
new[] { "Foo", "Baz" }
From above. I could build a SQL parser, but I have an open connection to SQL server, so I'd prefer to use an existing option if possible.
Edit:
There has to be a way to do this, because SQL Server's Business Intelligence Development Studio is able to do this very successfully.
Edit 2:
SQL BIDS is executing this command in order to describe the results:
exec sp_executesql N'SET FMTONLY OFF;SET FMTONLY ON;SELECT @Foo [Foo], ''@Bar'' [Bar], @Baz [Baz]',
N'@Foo sql_variant,@Baz sql_variant',
@Foo=NULL,@Baz=NULL
Which explains how it can determine the columns, but it may be just string parsing to get the parameters...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…