December 12, 2006 at 10:06 am
I cannot figure out how to use a parameter within a qualified identifier to reference a column name. In the shipping rates table there are 150 columns named 1,2,3,4,5,6, etc. I'm trying to create a stored procedure that takes a column name as a parameter passed in as an integer. When I try to concatenate the parameter into the Brackets it doesn’t recognize it. If I don’t use the brackets it doesn’t return the real value. It works in the vb.net code like this:
sqlcmd.CommandText = "SELECT [" & CInt(Weight) & "] FROM ShippingRates WITH (NOLOCK) WHERE ShipMethod='" & theShipMethod & "' AND CountryCode='" & dc.CountryCode & "' AND Zone=" & DestinationZone
If anyone knows how to do this in Transact-SQL I'd appreciate it.
Thank you,
Debra
December 12, 2006 at 10:13 am
Debra
The best solution to your problem would be to redesign your database so that it's properly normalised. If you can't do that, the only option you have is to use dynamic SQL. Search on this site for dynamic SQL to learn about the implications of doing this.
John
December 12, 2006 at 10:18 am
Can you please post the sql for the stored proc.
December 12, 2006 at 11:15 am
December 12, 2006 at 11:20 am
I'd strongly suggest you rethink the design. This query is supposed to be very simple!!
December 12, 2006 at 11:23 am
I'd like to change the column names but didn't design this database, so can't do that, unfortunately. Thanks.
December 12, 2006 at 11:25 am
How many distinct groups of columns can be selected from this table?
December 12, 2006 at 11:36 am
Not sure what you mean. Each column can be selected. This query works just fine in retrieving the value.
select top 1 [5] from shippingrates with (NOLOCK)
returns 8.35
this doesn't work: @Weight = 5 for column named 5
select top 1 [@Weight] from shippingrates with (NOLOCK)
returns 5
December 12, 2006 at 11:42 am
sorry, let me correct that.
select
top 1 @Weight from shippingrates WITH (NOLOCK)
returns 5
select top 1 [@Weight] from shippingrates with (NOLOCK)
returns
Msg 207, Level 16, State 1, Line 4
Invalid column name '@Weight'.
December 12, 2006 at 11:43 am
And it shouldn't work that way either. I'd strongly suggest you read the 3 articles when you get a few hours of free time. However to solve that particular solution there seem to be only one way :
DECLARE @sql VARCHAR(8000)
SET @sql = 'SELECT ' + @ColsList + ' FROM dbo.YourTable WHERE SomeCol = ' + STR(@Parm)
EXEC (@SQL)
December 12, 2006 at 2:12 pm
Problem is:
@Weight char(50),
[ + @Weight + ]
becomes
[5 ]
Is there such column in your table?
Always use PRINT for dynamic SQL before going to EXEC.
_____________
Code for TallyGenerator
December 12, 2006 at 3:37 pm
@weight varchar(20)
@weight = 5
@sql varchar(8000)
@sql = 'select [' + @weight + '] from shippingrates with (NOLOCK)'
returns: select [5] from shippingrates with (NOLOCK)
exec @sql
returns:
Msg 203, Level 16, State 2, Line 8
The name 'select [5] from shippingrates with (NOLOCK)' is not a valid identifier.
Oh well, thanks anyway, I'll read up on dynamic sql.
December 12, 2006 at 3:41 pm
Debra, take a break and spend several minutes learning SQL syntax.
declare @weight varchar(20)
set @weight = 5
declare @sql varchar(8000)
set @sql = 'select [' + @weight + '] from shippingrates with (NOLOCK)'
PRINT @sql
EXEC (@sql)
_____________
Code for TallyGenerator
December 12, 2006 at 3:46 pm
If you read Ninja's links,
http://www.sommarskog.se/dynamic_sql.html
http://www.sommarskog.se/dyn-search.html
http://www.sommarskog.se/arrays-in-sql.html
you'd find out that your syntax should be..
EXEC('
SELECT [' + @Weight + '] FROM ShippingRates WITH (NOLOCK) WHERE ShipMethod=''' + @theShipMethod + ''' AND CountryCode=''' + @CountryCode + ''' AND Zone=' + @DestinationZone
)
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply