简体   繁体   中英

SQL Server 2008 query into text file: bcp, dynamic SQL, temp tables and table variables

I am adding a section to some vendor code to create a text file from data in a temp table and send it to a network location. The temp table is #PickList

I've created a table type for the table variable, so I can pass it as a parameter to the dynamic SQL, but this in turn needs to be passed to bcp which creates the text file. This is what I have so far:

DECLARE @strFileLocation VARCHAR(1000)
DECLARE @strFileName VARCHAR(1000)
DECLARE @bcpCommand VARCHAR(8000)
DECLARE @strSQL VARCHAR(2000)

SET @strFileLocation = '\\phaal\FTP\LocalUser\LIFT01\inbox\'

SET @strFileName = 'BPL' + @Job + '-' + CAST(@Suffix AS VARCHAR(20)) + '-' +
CAST(@StartingOperNum AS VARCHAR(20)) + CAST(DATEPART(hh,GETDATE()) AS
            VARCHAR(10)) +  CAST(DATEPART(mi,GETDATE()) AS VARCHAR(10)) +   
CAST(DATEPART(ss,GETDATE()) AS VARCHAR(10)) + '.txt'

DECLARE @tblLeanLiftData AS [dbo].[BWT_LeanLiftPickTableType]

INSERT INTO @tblLeanLiftData (intSeq, strText)
SELECT  0,  @Job + '-' + CAST(@Suffix AS VARCHAR(20)) + '-' + 
CAST(@StartingOperNum AS VARCHAR(20))
UNION
SELECT  det_JobSequence, det_JobMatlItem + ':' + det_LotDescription + ',-,' +
CAST(det_QtyToPick AS VARCHAR(20))
FROM    #PickList

SET @strSQL = 'EXEC sp_executesql N''SELECT strText FROM @tblLeanLiftData
ORDER BY intSeq'', N''@tblLeanLiftData LeanLiftPickTableType READONLY'',
                                  @tblLeanLiftData=@tblLeanLiftData'

SET @bcpCommand = 'bcp "' + @strSQL + '" queryout "'

SET @bcpCommand = @bcpCommand + @strFileLocation + @strFileName + '" -T -c'

EXEC master..xp_cmdshell @bcpCommand

When I exec the code, I get:

SQLState = 37000, NativeError = 137
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Must declare the scalar variable "@tblLeanLiftData".
SQLState = 37000, NativeError = 8180
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could not be prepared.
NULL

So, the table variable is still out of scope.

I was hoping someone with fresh eyes could spot where I've gone wrong or an alternative route or if it's just not possible this way?

It's a bit of a double-edged sword, because I've tried various ways and either the temp table or table variable ends up being out of scope.

can you please try this?

DECLARE @strFileLocation VARCHAR(1000)
DECLARE @strFileName VARCHAR(1000)
DECLARE @bcpCommand VARCHAR(8000)
DECLARE @strSQL VARCHAR(2000)

SET @strFileLocation = '\\phaal\FTP\LocalUser\LIFT01\inbox\'

SET @strFileName = 'BPL' + @Job + '-' + CAST(@Suffix AS VARCHAR(20)) + '-' +
CAST(@StartingOperNum AS VARCHAR(20)) + CAST(DATEPART(hh,GETDATE()) AS
            VARCHAR(10)) +  CAST(DATEPART(mi,GETDATE()) AS VARCHAR(10)) +   
CAST(DATEPART(ss,GETDATE()) AS VARCHAR(10)) + '.txt'

INSERT INTO [dbo].[BWT_LeanLiftPickTableType] (intSeq, strText)
SELECT  0,  @Job + '-' + CAST(@Suffix AS VARCHAR(20)) + '-' + 
CAST(@StartingOperNum AS VARCHAR(20))
UNION
SELECT  det_JobSequence, det_JobMatlItem + ':' + det_LotDescription + ',-,' +
CAST(det_QtyToPick AS VARCHAR(20))
FROM    #PickList

SET @strSQL = 'EXEC sp_executesql N''SELECT strText FROM [dbo].[BWT_LeanLiftPickTableType]
ORDER BY intSeq'', N''[dbo].[BWT_LeanLiftPickTableType] LeanLiftPickTableType READONLY'',
                                  [dbo].[BWT_LeanLiftPickTableType]=[dbo].[BWT_LeanLiftPickTableType]'

SET @bcpCommand = 'bcp "' + @strSQL + '" queryout "'

SET @bcpCommand = @bcpCommand + @strFileLocation + @strFileName + '" -T -c'

EXEC master..xp_cmdshell @bcpCommand        

If this suceeds, then the only issue is that you tried to use a variable as a column name. Also as I have seen from your query it is not completely necessary to use the table name as a variable, as you declare and assign it immediately, so you could just as well write your column there. A thing to add if you choose to do this way is to truncate the table at the end of the query.

If the intention was to create a table variable, it should be assigned like this:

DECLARE @strFileLocation VARCHAR(1000)
DECLARE @strFileName VARCHAR(1000)
DECLARE @bcpCommand VARCHAR(8000)
DECLARE @strSQL VARCHAR(2000)

SET @strFileLocation = '\\phaal\FTP\LocalUser\LIFT01\inbox\'

SET @strFileName = 'BPL' + @Job + '-' + CAST(@Suffix AS VARCHAR(20)) + '-' +
CAST(@StartingOperNum AS VARCHAR(20)) + CAST(DATEPART(hh,GETDATE()) AS
            VARCHAR(10)) +  CAST(DATEPART(mi,GETDATE()) AS VARCHAR(10)) +   
CAST(DATEPART(ss,GETDATE()) AS VARCHAR(10)) + '.txt'

DECLARE @tblLeanLiftData TABLE (intSeq int, strText VARCHAR(MAX))

INSERT INTO @tblLeanLiftData (intSeq, strText)
SELECT  0,  @Job + '-' + CAST(@Suffix AS VARCHAR(20)) + '-' + 
CAST(@StartingOperNum AS VARCHAR(20))
UNION
SELECT  det_JobSequence, det_JobMatlItem + ':' + det_LotDescription + ',-,' +
CAST(det_QtyToPick AS VARCHAR(20))
FROM    #PickList

SET @strSQL = 'EXEC sp_executesql N''SELECT strText FROM @tblLeanLiftData
ORDER BY intSeq'', N''@tblLeanLiftData LeanLiftPickTableType READONLY'',
                                  @tblLeanLiftData=@tblLeanLiftData'

SET @bcpCommand = 'bcp "' + @strSQL + '" queryout "'

SET @bcpCommand = @bcpCommand + @strFileLocation + @strFileName + '" -T -c'

EXEC master..xp_cmdshell @bcpCommand

I hope this answer will help you to move forward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM