Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9314493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:08:48+00:00 2026-06-19T02:08:48+00:00

Using SQL Server 2008, I have this query which works the first time I

  • 0

Using SQL Server 2008, I have this query which works the first time I run it, but if I try to run it again, I get an error

if (exists (select * from tempdb.INFORMATION_SCHEMA.TABLES where TABLE_NAME = '##a12'))
begin
drop table ##a12
end
else
declare @p varchar(1000)
declare @s varchar(5)
declare @xx2 varchar(2000)

set @p = '64-267-601'
select top 1  @p=ivproduct from rpiv where upc=@p or ivproduct=@p
select distinct storeid,color,wil,cast('OH' as varchar(4)) as TP into ##a12 from rpiv i, rpproducts p where i.ivproduct=p.productid and p.productid=@p
insert into ##a12 select storeid,color,wil,'Rcvd' from ##a12
insert into ##a12 select storeid,color,wil,'Sld' from ##a12 where tp='oh'

DECLARE myCursor CURSOR FOR SELECT distinct size from rpiv where ivproduct=@p
open MyCursor
FETCH NEXT FROM myCursor into @S
WHILE @@FETCH_STATUS = 0
    begin
    set @xx2='alter table ##a12 add ['+@s+'] varchar(5)'
    exec(@xx2)
    set @xx2='update a set a.['+@s+']=coalesce(b.onhand,0) from ##a12 a,rpiv b where a.tp=''oh'' and a.storeid=b.storeid and a.color=b.color and a.wil=b.wil and b.size='''+@s+''''
    exec(@xx2)
    set @xx2='update a set a.['+@s+']=coalesce(b.PurchasedToDate,0) from ##a12 a,rpiv b where  a.tp=''rcvd'' and a.storeid=b.storeid and a.color=b.color and a.wil=b.wil and b.size='''+@s+''''
    exec(@xx2)
    set @xx2='update a set a.['+@s+']=coalesce(b.SoldtoDate,0) from ##a12 a,rpiv b where  a.tp=''sld'' and a.storeid=b.storeid and a.color=b.color and a.wil=b.wil and b.size='''+@s+''''
    exec(@xx2)
    set @xx2='update ##a12 set ['+@s+']='''' where ['+@s+'] =''0'''
    exec(@xx2)
    set @xx2='update ##a12 set ['+@s+']='''' where ['+@s+'] is null'
    exec(@xx2)

    FETCH NEXT FROM myCursor into @S
    End
Close myCursor
DEALLOCATE myCursor
if not exists(select * from ##a12 where wil<>'') 
begin
alter table ##a12 drop column wil
select * from ##a12 order by storeid,color,tp
end
else
select * from ##a12 order by storeid,color,wil,tp

And this is the error that I get :

Msg 207, Level 16, State 1, Line 14
Invalid column name 'wil'.
Msg 213, Level 16, State 1, Line 14
Column name or number of supplied values does not match table definition.

If I reload my Management studio, it works just fine, and if I manually drop the table it works as well. I have used that if > drop statement at the start before with no problem, but for some reason it is not taking it now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:08:49+00:00Added an answer on June 19, 2026 at 2:08 am

    Try adding a GO:

    Close myCursor
    DEALLOCATE myCursor
    GO
    
    if not exists(select * from ##a12 where wil<>'') 
    begin
    

    This is happening because when the batch gets parsed on the second execution, the wil column has been dropped, and the parser doesn’t realize that it will be re-added when the temporary table is dropped and recreated.

    By adding the GO, you split the batch into two… and when the second batch runs, the wil column exists again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 R2 I have a complex query which I need
So I'm working on SQL Server 2008 and I have this query which should
Using Microsoft SQL server 2008 I have a query that is in need of
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
We are using SQL Server 2008. We have a table called response which has
I've been using the MERGE operation in SQL Server 2008, but have now come
I'm using sql server 2008. I'm doing a query which is going to be
Using sql server 2008, I have a simple stored procedure, the contents of which
I'm using SQL Server 2008 R2 (T-SQL) and have dynamic SQL code, which inserting
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.