sp_execute espera el parámetro '@handle' del tipo 'int'


9

Estoy tratando de verificar un procedimiento almacenado si existe una tabla en mi base de datos de destino. Si no es así, crearé la tabla utilizando las tablas de información_esquema de la base de datos de origen. Sin embargo, cuando uso sp_execute para intentar recuperar si la tabla existe, recibo el error. El procedimiento espera el parámetro '@handle' del tipo 'int'.

No estoy usando un parámetro @handle. ¿Alguien puede decirme qué significa este error y por qué lo estoy recibiendo? La porción relevante de mi código está abajo.

DECLARE @SQL NVARCHAR(MAX),
        @Parameters NVARCHAR(4000),
        @TableNotExists INT,
        @SourceTable NVARCHAR(200),
        @DestDB NVARCHAR(200)

BEGIN

SET @SourceTable = 'table'
SET @DestDB = 'database'
SET @Parameters = N'@SourceTableIN NVARCHAR(200), @TableNotExistsOut INT OUTPUT'
SET @SQL = N'USE [' + @DestDB + '] IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ''BASE TABLE'' AND TABLE_NAME = @SourceTableIN)
BEGIN SET @TableNotExistOUT = 1 END'

EXEC sp_Execute @SQL, @Parameters, @SourceTableIN = @SourceTable, @TableNotExistsOUt = @TableNotExists OUTPUT

END

44
¿Querías llamar en sp_executesqllugar de sp_execute?
Philᵀᴹ

La variable @TableNotExistsOutse escribe incorrectamente dentro del texto SQL.
Jon Seigel

Respuestas:


11

Creo que querías usar sp_executesql:

EXEC sp_Executesql @SQL, @Parameters, @SourceTableIN = @SourceTable, @TableNotExistsOut = @TableNotExists OUTPUT

Y como JonSeigel señaló en el comentario, ha escrito mal un parámetro en su declaración:

SET @SQL = N'USE [' + @DestDB + '] IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ''BASE TABLE'' AND TABLE_NAME = @SourceTableIN)
BEGIN SET @TableNotExistOUT = 1 END'

Eso debería ser @TableNotExistsOUT.

Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.