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

Sintaxis del comando BCP

Estas en el tema de Sintaxis del comando BCP en el foro de SQL Server en Foros del Web. Hola si alguien me puede auxiliar en el siguiente problema, se los agradecere mucho. En este script ocupo el comando bcp para extraer información de ...
  #1 (permalink)  
Antiguo 01/02/2008, 17:34
 
Fecha de Ingreso: noviembre-2007
Mensajes: 9
Antigüedad: 16 años, 5 meses
Puntos: 0
Sonrisa Sintaxis del comando BCP

Hola si alguien me puede auxiliar en el siguiente problema, se los agradecere mucho.

En este script ocupo el comando bcp para extraer información de una tabla, armo toda la sentencia en una variable (@query) que interpreta el comando y manda la información a un archivo de texto.

El proble que tengo, es que en esa instrucción de sql especificamente en la clausula where quiero ocupar variables para condicionar el resultado y aunque se encuentran declaradas me manda errores como: "la variable debe declararse", etc y si le pongo el valor dierectamente, se hace pelotas con las comillas. Este es el script.

esta es la parte donde truna el código:
where PersonnelTypeDescription = @Type and employeenumber=@employ, si se dan cuenta las variables ya están declaradas.

Declare @employ varchar(12),
@Type varchar(12)
SET @employ = '2002528'
SET @Type = 'permanente'

--Select employeenumber, Convert(char(12), Convert(SmallDateTime, hostdate - 2), 103) as fecha, Convert(char(8), Convert(SmallDateTime, hostdate -2), 108) as Hora, readerdescription From SecurePerfectHistory.dbo.BadgeHistoryTable where PersonnelTypeDescription = @Type and employeenumber = @employ and hostdate >= (DATEADD(day, -28, getdate())) + 2
DECLARE @FileName varchar(50)
Declare @archivo_txt varchar(100)
Declare @administrador_SQL varchar(50)
Declare @password_sql varchar(50)
Declare @base_datos varchar(50)

--- configurar estos datos de acuerdo al servidor
SET @FileName = REPLACE('c:\SecurePerfect_'+CONVERT(char(8),GETDAT E(),1)+'.txt','/','-')
Set @administrador_SQL='sa'
Set @password_sql=''
Set @base_datos='SecurePerfectHistory'

declare @query varchar(6000)

--set @query = 'Select employeenumber, Convert(char(12), Convert(SmallDateTime, hostdate - 2), 103) as fecha, Convert(char(8), Convert(SmallDateTime, hostdate -2), 108) as Hora, readerdescription From SecurePerfectHistory.dbo.BadgeHistoryTable where PersonnelTypeDescription = @Type and employeenumber=@employ and hostdate >= (DATEADD(day, -28, getdate())) + 2'
set @query = 'Select employeenumber, Convert(char(12), Convert(SmallDateTime, hostdate - 2), 103) as fecha, Convert(char(8), Convert(SmallDateTime, hostdate -2), 108) as Hora, readerdescription From SecurePerfectHistory.dbo.BadgeHistoryTable where hostdate >= (DATEADD(day, -28, getdate())) + 2 AND PersonnelTypeDescription = @Type'

--- obtiene el número de registros procesados
exec (@query)
declare @registros int
set @registros = @@rowcount

--- Comando bcp para generar el archivo de texto plano.
DECLARE @chr_BCP_ARCHIVO VARCHAR(4000)
SELECT @chr_BCP_ARCHIVO = 'bcp "' + @query + '" queryout '+ @FileName + ' -c -U -t,'
EXEC master..xp_cmdshell @chr_BCP_ARCHIVO
  #2 (permalink)  
Antiguo 01/02/2008, 19:32
Avatar de iislas
Colaborador
 
Fecha de Ingreso: julio-2007
Ubicación: Mexico, D.F.
Mensajes: 6.482
Antigüedad: 16 años, 10 meses
Puntos: 180
Re: Sintaxis del comando BCP

Para solvertar tu problema, debes olvidarte de la instrucion EXEC (que es vieja) y ocupar el store SP_EXECUTESQL que acepta parametros y manda resultados como una funcion:

sp_executesql permite que se establezcan valores en los parámetros de forma separada de la cadena de Transact-SQL:

DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)

/* Build the SQL string once. */
SET @SQLString =
N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
/* Specify the parameter format once. */
SET @ParmDefinition = N'@level tinyint'

/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable
/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable
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 13:30.