Kenzo
Kenzo

Reputation: 1837

Use a varible to make a table name in sql command text

I want to make an sql command text query in which i pass a variable named m as a part of the table name. This is what i have tried but seems not to be working :

sqlCommand.CommandText = 
'SELECT ProfileId, FloorId, ZoneId, mapHierarchyString, x, y 
FROM DynamicPosition'+@tableNumber+' 
WHERE ProfileId='''+@profileId+''';

Upvotes: 0

Views: 155

Answers (2)

mkRabbani
mkRabbani

Reputation: 16908

You have some syntax issue in the script. Try with this following scritp-

sqlCommand.CommandText = 
'
SELECT FloorId, ZoneId FROM DynamicPosition'+@m+'  
WHERE ProfileId='''+@profileId+''' AND FloorId = '+@floorId+'
';
--Assuming FloorId are Number

You code will be as below-

sqlCommand.CommandText = 
'SELECT ProfileId, FloorId, ZoneId, mapHierarchyString, x, y 
FROM DynamicPosition'+@tableNumber+' 
WHERE ProfileId='''+@profileId+'''';

Upvotes: 1

dfundako
dfundako

Reputation: 8314

It looks like it is interpreting the @m literally because it is within the quotes. Put it outside them and make the whole thing two strings:

sqlCommand.CommandText = "SELECT FloorId, ZoneId, FROM DynamicPosition" + @m + " WHERE ProfileId='WIFI5'";

Upvotes: 1

Related Questions