简体   繁体   中英

Invalid column name: SQL Server 2008 R2

I have a stored procedure I use to extract binary data from SQL tables to create physical files. I have used it a couple of times on tables with the same structure restored from SQL backups, and it works OK.

Today I wanted to extract some binary data from a new table inside a restored backup. I opened the stored procedure I have been using and set about altering the code. Once I was happy with the changes I tried to execute the 'ALTER' statement. Unfortunetly, both of the column names I have used are 'invalid' despite existing on the the 'Document' table.

I have read a number of other threads regarding 'invalid column name' errors, but the majority of these seem to be typing errors. I've checked my column names numurous times (intelli sense even lets me put in 'Document.Document_ID' and 'Document.Document_Filename' but they still fail).

Any ideas where I am going wrong?

Source:

USE [Example Live]
GO
/****** Object:  StoredProcedure [dbo].[FileExport]    Script Date: 10/18/2012 11:42:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Chris Murray
-- Create date: 18/10/2012
-- Description: Exports binary file data
-- =============================================
ALTER PROCEDURE [dbo].[FileExport] 
-- Add the parameters for the stored procedure here
@OutputFilePath VARCHAR(500) = 'C:\Conv\Example\In\Afiles\'
AS
BEGIN

   DECLARE @totrow int
   DECLARE @currow int
   DECLARE @result int
   DECLARE @nsql nvarchar(4000)

   DECLARE @sqlStatements table (ID int IDENTITY(1, 1),  SqlStatement varchar(max))   

   INSERT
   INTO @sqlStatements
   SELECT 'BCP "SELECT Document_Data FROM [Example Live].[dbo].[Document] WHERE Document_ID = '''
  + CAST(Document_ID AS VARCHAR(500)) + '''" queryout ' + @OutputFilePath 
  + CAST(Document_Filename AS VARCHAR(500)) + ' -S localhost\SQLEXPRESS2008 -T -f C:\Conv\Example\In\AFiles\Images.fmt'
   FROM dbo.Photograph  

   SET @totrow = @@ROWCOUNT
   SET @currow = 1
   WHILE @totrow > 0 and @currow <= @totrow
   BEGIN
      SELECT @nsql = SqlStatement
      FROM @sqlStatements
      WHERE ID = @currow
      EXEC @result = xp_cmdshell @nsql
      SET @currow = @currow + 1
   END
END

Thanks for the ideas everyone.

The problem was of my own making, and a rookie mistake. I neglected to point the SELECT statement at the correct table..

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