Foros del Web » Programando para Internet » PHP »

Problemas Al Declarar Un Query De Mssql En Php

Estas en el tema de Problemas Al Declarar Un Query De Mssql En Php en el foro de PHP en Foros del Web. Este es el query que quiero usar en Php, y nose como declarar las variables de sql, para que php lo entienda, espero su ayuda. ...
  #1 (permalink)  
Antiguo 23/11/2007, 09:08
Avatar de viqtor  
Fecha de Ingreso: noviembre-2007
Ubicación: Donde estoy?
Mensajes: 49
Antigüedad: 16 años, 5 meses
Puntos: 0
Problemas Al Declarar Un Query De Mssql En Php

Este es el query que quiero usar en Php, y nose como declarar las variables de sql, para que php lo entienda, espero su ayuda.

set dateformat dmy;
declare @FI smalldatetime
declare @FF smalldatetime
declare @vendedor char(5)
declare @producto char (13)
set @FI = '01/10/2007 00:00'
set @FF = '22/11/2007 23:59'
set @vendedor = '15'
set @producto = '1'

SELECT C.cod_cte, C.razon_social,
isnull((select top 1 fecha
from entysal where status <> 'C' and fecha between @FI and @FF and cod_cte = C.cod_cte
and vendedor = @vendedor and cod_prod = @producto and transaccion in ('36','37','38')
order by fecha desc),'') as ultima_venta,
isnull((select top 1 fecha
from mpedcte where status <> 'C' and fecha between @FI and @FF and cod_cte = C.cod_cte
and vendedor = @vendedor and cod_prod = @producto and transaccion = '31'
order by fecha desc),'') as ultima_pedido,
isnull((select sum(cantidad) from entysal where folio in
(select top 1 folio
from entysal where status <> 'C' and fecha between @FI and @FF and cod_cte = C.cod_cte
and vendedor = @vendedor and cod_prod = @producto and transaccion in ('36','37','38')
order by fecha desc)),0.0) as cantidad_venta,
isnull((select sum(cantidad_pedida) from mpedcte where folio in
(select top 1 folio
from mpedcte where status <> 'C' and fecha between @FI and @FF and cod_cte = C.cod_cte
and vendedor = @vendedor and cod_prod = @producto and transaccion = '31'
order by fecha desc)),0.0) as cantidad_pedido,
isnull((select status from pedcte where folio in
(select top 1 folio
from mpedcte where status <> 'C' and fecha between @FI and @FF and cod_cte = C.cod_cte
and vendedor = @vendedor and cod_prod = @producto and transaccion = '31'
order by fecha desc)),'') as status_pedido
FROM CLIENTES C
where vendedor = @vendedor

MUACHAS GRACIAS
  #2 (permalink)  
Antiguo 23/11/2007, 09:23
Avatar de Kelpie  
Fecha de Ingreso: febrero-2002
Ubicación: NorthSpain
Mensajes: 609
Antigüedad: 22 años, 2 meses
Puntos: 8
Re: Problemas Al Declarar Un Query De Mssql En Php

Podrías probar esto:

$FI = '01/10/2007 00:00';
$FF = '22/11/2007 23:59';

$vendedor = '15';
$producto = '1';


$sqt="SELECT C.cod_cte, C.razon_social,";
$sqt.="isnull((select top 1 fecha";
$sqt.=" from entysal where status <> 'C' and fecha between '$FI' and '$FF' and cod_cte = C.cod_cte";
$sqt.=" and vendedor = '$vendedor' and cod_prod = @producto and transaccion in ('36','37','38')";
$sqt.=" order by fecha desc),'') as ultima_venta,";
$sqt.="isnull((select top 1 fecha";
$sqt.=" from mpedcte where status <> 'C' and fecha between '$FI' and '$FF' and cod_cte = C.cod_cte";
$sqt.=" and vendedor = '$vendedor' and cod_prod = '$producto' and transaccion = '31'";
$sqt.=" order by fecha desc),'') as ultima_pedido,";
$sqt.="isnull((select sum(cantidad) from entysal where folio in";
$sqt.=" (select top 1 folio";
$sqt.=" from entysal where status <> 'C' and fecha between '$FI' and '$FF' and cod_cte = C.cod_cte";
$sqt.=" and vendedor = '$vendedor' and cod_prod = '$producto' and transaccion in ('36','37','38')";
$sqt.=" order by fecha desc)),0.0) as cantidad_venta,";
$sqt.="isnull((select sum(cantidad_pedida) from mpedcte where folio in";
$sqt.=" (select top 1 folio";
$sqt.=" from mpedcte where status <> 'C' and fecha between @FI and @FF and cod_cte = C.cod_cte";
$sqt.=" and vendedor = '$vendedor' and cod_prod = '$producto' and transaccion = '31'";
$sqt.=" order by fecha desc)),0.0) as cantidad_pedido,";
$sqt.="isnull((select status from pedcte where folio in";
$sqt.=" (select top 1 folio";
$sqt.=" from mpedcte where status <> 'C' and fecha between @FI and @FF and cod_cte = C.cod_cte";
$sqt.=" and vendedor = '$vendedor' and cod_prod = '$producto' and transaccion = '31'";
$sqt.=" order by fecha desc)),'') as status_pedido";
$sqt.=" FROM CLIENTES C";
$sqt.=" where vendedor = '$vendedor'";

$res=mssql_query($sqt);
...

Creo que no me he dejado ningún espacio. Las fechas SQL server, ¿las puede tomar como cadenas con el formato 'dd/mm/yyyy hh:nn'? Si no habría que cambiar las líneas en azul, por ejemplo, en formato yyyymmddhhnn serían:

$FI = '200710010000';
$FF = '200711222359';
__________________
Kelpie
  #3 (permalink)  
Antiguo 23/11/2007, 09:29
Avatar de ferbux  
Fecha de Ingreso: mayo-2007
Ubicación: por ahí intentado ayudar
Mensajes: 823
Antigüedad: 17 años
Puntos: 6
Re: Problemas Al Declarar Un Query De Mssql En Php

Hola viqtor, para ejecutar una consulta con php y mysql lo que tienes que hacer es:

1.- crear una conexion entre tu formulario en php y mysql:
Código:
                    $link = mysql_connect("server", "root", "clave_de_acceso_a_tu_bd");
                      mysql_select_db("nombre_de_tu_BD", $link);
2.- Luego debes de hacer tu SELECT mas o menos de la siguiente forma:
Código:
 $sel="SELECT campo1, campo2, .... FROM nombre_de_tu_tabla WHERE alguna_condicion";
         $resultado = mysql_query($sel) or die('Consulta fallida: ' . mysql_error());
			  $row=mysql_fetch_array($resultado);
$sel seroia la variable en la cual guardas tu consulta para ejecutarla.

$resultado, ahi almacenas el resultado que te arroja al ejecutar tu consuta.

$row, aki se almacenan los valores obtenidos en $resultado en forma de un arreglo.
__________________
"Eres grande por que caminas entre gigantes"
  #4 (permalink)  
Antiguo 23/11/2007, 12:32
Avatar de viqtor  
Fecha de Ingreso: noviembre-2007
Ubicación: Donde estoy?
Mensajes: 49
Antigüedad: 16 años, 5 meses
Puntos: 0
Re: Problemas Al Declarar Un Query De Mssql En Php

Buen dia:
Puse el query como me dijiste pero me regresa estos errores

??
Cita:
Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near the keyword 'and'. (severity 15) in C:\AppServ\www\areacomercial.starfruts.com.mx\vend edor01.php on line 133

Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near the keyword 'and'. (severity 15) in C:\AppServ\www\areacomercial.starfruts.com.mx\vend edor01.php on line 133

Warning: mssql_query() [function.mssql-query]: Query failed in C:\AppServ\www\areacomercial.starfruts.com.mx\vend edor01.php on line 133

Warning: mssql_query() [function.mssql-query]: Query failed in C:\AppServ\www\areacomercial.starfruts.com.mx\vend edor01.php on line 136

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in C:\AppServ\www\areacomercial.starfruts.com.mx\vend edor01.php on line 137
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 15:46.