Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    188

    Unanswered: Invalid Column Error

    This is my syntax, I have removed then added back line by line by line and determined it is the insert of the variable into the table that skews. Can someone with more SQL Server skills show me the err in my ways?
    Code:
    Create Table #Table1 (ID Int Identity, p nvarchar(20))
    Create Table #Table2 (date datetime, salesID int, p varchar(20))
    
    Insert into #Table1 Values ('ZeroWireless')
    
    Declare @Str nvarchar(4000), @p nvarchar(20)
    Select @p = p
    From #Table1
    
    Set @Str =
    ' Insert Into #Table2 (date, salesID, p)
    Select saleDate, salesID, '+@p+'
    FROM brimstone.dbo.totals
    Exec SP_ExecuteSql @Str
    
    SELECT * from #Table2

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    I am assuming that you have checked the brimstone.dbo.totals table to make sure that it has a ZeroWireless column? Or did you want 'ZeroWireless' to be inserted into the table? If you want the string 'ZeroWireless' to be inserted, you just need to make sure it has quotes around it in the @Str variable:
    Code:
    Set @Str =
    ' Insert Into #Table2 (date, salesID, p)
    Select saleDate, salesID, '''+@p+'''
    FROM brimstone.dbo.totals'
    print @Str

  3. #3
    Join Date
    Feb 2012
    Posts
    188
    Ah - triple single quotes! That's what it was needing.

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    Well, double triple single quotes to be exact, since you need them at both ends. ;-)

    (sorry. Couldn't resist)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •