I am facing a problem while using dynamic sql , i want the result back in local variable from dynamic sql execution but sql server 2000 not able to given results in local variable b'coz of out of scope(context of dynamic sql execution is sepetate) , alternativly i am using SELECT INTO clause and taking output in a temporary table.But its a costly operation , since these dyanmic statement i am using lacs of times in data migration from dbf to sql server 2000.
I will be greatful if anybody suggest any other cheaper method of taking output into local variable from dynamic sql.
Sample SP which i am executing is as given below -
----------------------------------------------------------
CREATE PROCEDURE dbo.GetForeignOID
@PKTableName varchar(30),
@PKBKName varchar(30),
@PKBKValue varchar(30),
@ForeignOID varchar(32)
OUTPUT AS
Declare @CmdString nvarchar(500)
Declare @ReturnVal bit
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME = 'RETOID' and XTYPE = 'U' )
DROP TABLE RETOID
SET @CmdString = N'SELECT OID INTO RETOID FROM '+@PKTableName + ' WITH (NOLOCK) WHERE ' + @PKBKName + ' = ''' + @PKBKValue + ''' AND DELETEOID = ''0'''
EXEC sp_executesql @CmdString
SET @ForeignOID = 0
SELECT @ForeignOID = ISNULL(OID,0) FROM RETOID
IF @ForeignOID = 0
SET @ForeignOID = null
RETURN
--------------------------------------------------------
I will be greatful if anybody suggest any other cheaper method of taking output into local variable from dynamic sql.
Sample SP which i am executing is as given below -
----------------------------------------------------------
CREATE PROCEDURE dbo.GetForeignOID
@PKTableName varchar(30),
@PKBKName varchar(30),
@PKBKValue varchar(30),
@ForeignOID varchar(32)
OUTPUT AS
Declare @CmdString nvarchar(500)
Declare @ReturnVal bit
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME = 'RETOID' and XTYPE = 'U' )
DROP TABLE RETOID
SET @CmdString = N'SELECT OID INTO RETOID FROM '+@PKTableName + ' WITH (NOLOCK) WHERE ' + @PKBKName + ' = ''' + @PKBKValue + ''' AND DELETEOID = ''0'''
EXEC sp_executesql @CmdString
SET @ForeignOID = 0
SELECT @ForeignOID = ISNULL(OID,0) FROM RETOID
IF @ForeignOID = 0
SET @ForeignOID = null
RETURN
--------------------------------------------------------