Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » SQL Server »

Ayuda con Procedimiento de SQL

Estas en el tema de Ayuda con Procedimiento de SQL en el foro de SQL Server en Foros del Web. Hola bueno toy haciendo un procedimiento con varias consultas de sql en el cual yo voy un indicador que es @indi y dependiendo del numero ...
  #1 (permalink)  
Antiguo 01/05/2010, 22:30
 
Fecha de Ingreso: octubre-2009
Mensajes: 14
Antigüedad: 14 años, 6 meses
Puntos: 0
Ayuda con Procedimiento de SQL

Hola bueno toy haciendo un procedimiento con varias consultas de sql en el cual yo voy un indicador que es @indi y dependiendo del numero qe le envie me va a hacer la sentencia que se le indica pero el problema es que me sale error cuando quiero agregar el procedimiento me sale error en la sentencia If :S porfavor alguien me pueda ayudar ..!

Cita:
Create Proc usp_Procedimientos
@indi int,@Fec1 datetime,@Fec2 datetime,@Prod int,@Año int
if(@indi=1)
Set dateformat dmy
Select distinct O.OrderID,CompanyName,convert(varchar,OrderDate,10 3)as OrderDate,Sum(UnitPrice*Quantity)as Total
From Orders O,Customers C,[Order Details] Od
where O.OrderId=Od.OrderID and O.CustomerID=C.CustomerID and OrderDate between @Fec1 and @Fec2
Group by O.OrderID,CompanyName,OrderDate
else if(@indi=2)
Select O.OrderID,CompanyName,convert(varchar,OrderDate,10 3)as OrderDate,Sum(UnitPrice*Quantity)as Total
From Orders O,Customers C,[Order Details] Od
where O.OrderId=Od.OrderID and O.CustomerID=C.CustomerID and ProductID = @Prod
Group by O.OrderID,CompanyName,OrderDate
else if(@indi=3) then
Select O.OrderID,CompanyName,convert(varchar,OrderDate,10 3)as OrderDate,Sum(UnitPrice*Quantity)as Total
From Orders O,Customers C,[Order Details] Od
where O.OrderId=Od.OrderID and O.CustomerID=C.CustomerID and year(OrderDate)=@Año
Group by O.OrderID,CompanyName,OrderDate
Go
  #2 (permalink)  
Antiguo 03/05/2010, 07:05
 
Fecha de Ingreso: enero-2008
Ubicación: Santiago
Mensajes: 49
Antigüedad: 16 años, 3 meses
Puntos: 4
Respuesta: Ayuda con Procedimiento de SQL

A simple vista, te faltan todos los Begin y End correspondientes, prueba con algo asi...

Create Proc usp_Procedimientos
@indi int,@Fec1 datetime,@Fec2 datetime,@Prod int,@Año int
as
begin
if (@indi = 1)
begin
Set dateformat dmy
Select distinct O.OrderID,CompanyName,convert(varchar,OrderDate,10 3)as OrderDate,Sum(UnitPrice*Quantity)as Total
From Orders O,Customers C,[Order Details] Od
where O.OrderId=Od.OrderID and O.CustomerID=C.CustomerID and OrderDate between @Fec1 and @Fec2
Group by O.OrderID,CompanyName,OrderDate
end
else if(@indi=2)
begin
Select O.OrderID,CompanyName,convert(varchar,OrderDate,10 3)as OrderDate,Sum(UnitPrice*Quantity)as Total
From Orders O,Customers C,[Order Details] Od
where O.OrderId=Od.OrderID and O.CustomerID=C.CustomerID and ProductID = @Prod
Group by O.OrderID,CompanyName,OrderDate
end
else if(@indi=3)
begin
Select O.OrderID,CompanyName,convert(varchar,OrderDate,10 3)as OrderDate,Sum(UnitPrice*Quantity)as Total
From Orders O,Customers C,[Order Details] Od
where O.OrderId=Od.OrderID and O.CustomerID=C.CustomerID and year(OrderDate)=@Año
Group by O.OrderID,CompanyName,OrderDate
end
end
  #3 (permalink)  
Antiguo 03/05/2010, 08:06
Avatar de flaviovich  
Fecha de Ingreso: agosto-2005
Ubicación: Lima, Peru
Mensajes: 2.951
Antigüedad: 18 años, 8 meses
Puntos: 39
Respuesta: Ayuda con Procedimiento de SQL

EL begin... end es necesario cuando es mas de un comando.
Tienes un then demás: else if(@indi=3) then
Para la proxima pon el texto del error.
__________________
No repitamos temas, usemos el Motor de busquedas
Plantea bien tu problema: Ayúdanos a ayudarte.

Etiquetas: procedimiento, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:31.