Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am sending query in a string from linq to sql
C#
string _ObjCocktail = "declare @max_id int Insert into dbo.Cocktails(name,cocktail_category_id,instrustions) values ('Mojto',1,'Instruction of the cocktail') select @max_id = MAX(dbo.Cocktails.id) from dbo.Cocktails Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,2,null,1,null) Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,7,null,1,null)";
public void InsertCocktail()
        {
            try
            {
                 _DataContext.InsertCocktails(_ObjCocktail);
            }
            catch (Exception ex)
            {
                
            }
and

i am handling that string in sql like this.

SQL
ALTER procedure [dbo].[InsertCocktails]
(
@query varchar(max)
)
as
begin
exec @query
end
It is going to Exception with this error.
The name 'declare @max_id int Insert into dbo.Cocktails(name,cocktail_category_id,instrustions) values ('Mojto',1,'Instruction of the cocktail') select @max_id = MAX(dbo.Cocktails.id) from dbo.Cocktails Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,2,null,1,null) Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,7,null,1,null)' is not a valid identifier.

When i copy paste this query string in sql it is running fine.
Posted
Updated 28-Feb-12 7:23am
v2

1 solution

Change
SQL
exec @query

to
SQL
exec (@query)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900