|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
stored procedureI need to create a stored procedure like this : CREATE PROCEDURE croix @var1 varchar(50), @var2 varchar(50) AS SELECT @var1,@var2,sum(myvar) as nb from mytable group by @var1,@var2 GO I receive an error. What is the problem ? Thanks. you can not do it like that
Use sp_executesql oe exec() if you need to do this dynamically First read this (http://www.sommarskog.se/dynamic_sql.html) to see some of the risks Denis the SQL Menace http://sqlservercode.blogspot.com/ Hammou wrote: Show quote > Hello, > > I need to create a stored procedure like this : > > > CREATE PROCEDURE croix > @var1 varchar(50), > @var2 varchar(50) > AS > SELECT @var1,@var2,sum(myvar) as nb > from mytable > group by @var1,@var2 > GO > > > I receive an error. > > What is the problem ? > > > Thanks. > I receive an error. Could you be more specific? What error?> SELECT @var1,@var2,sum(myvar) as nb You cannot include variables in the GROUP BY. The GROUP BY is unnecessary > from mytable > group by @var1,@var2 anyway, because SQL Server will understand the variables are constants. SELECT @var1,@var2,sum(myvar) as nb from mytable A > I need to create a stored procedure like this : Are you trying to name the columns dynamically? It's not really clear what > > > CREATE PROCEDURE croix > @var1 varchar(50), > @var2 varchar(50) > AS > SELECT @var1,@var2,sum(myvar) as nb > from mytable > group by @var1,@var2 you are trying to accomplish. Aaron Bertrand [SQL Server MVP] a écrit :
Show quote >> I need to create a stored procedure like this : Thanks, the precedent response gived me the solution.>> >> >> CREATE PROCEDURE croix >> @var1 varchar(50), >> @var2 varchar(50) >> AS >> SELECT @var1,@var2,sum(myvar) as nb >> from mytable >> group by @var1,@var2 > > Are you trying to name the columns dynamically? It's not really clear what > you are trying to accomplish. > > |
|||||||||||||||||||||||