Solved

Trying to translate to use a table variable instead

Posted on 2008-06-19
11
188 Views
Last Modified: 2010-03-19
I need this in a table variable so a dataset can see it at design time.  

I tried converting it to use a table variable, but I kept getting errors on the update statements.  Is there somethiing I can't do here...

Select
  i.keyItem,  
  sum(iv.othQuantity) as othQuantity,
  p.othPar,
  Cast(0 as int) as keyVendor,
  Cast('' as varchar(100)) as nameVendor
Into #TempStore
From Item i
    Left Join Inventory iv on iv.keyItem = i.keyItem and iv.keyLocation = @keyLocation
    Left Join Par p on i.keyItem = p.keyItem and p.keyLocation = @keyLocation
Where (i.keyLocation = @keyLocation      
       or (i.keyLocation is null and i.keyCompany = @keyCompany))
  and  i.keyChildItem < 1
  and  i.sysActive = 1  
  and  othQuantity <= p.othPar
Group By
 i.keyItem,
 p.othPar

Update #TempStore
Set keyVendor =
(Select top 1 keyVendor
From PurchaseOrder p
      Inner Join PurchaseOrderItem po on p.keyPurchaseOrder = po.keyPurchaseOrder
Where po.keyItem = #TempStore.KeyItem
and   p.keyLocation = @keyLocation
Order by p.keyPurchaseOrder Desc)

Update #TempStore
Set nameVendor =
(Select nameVendor
From Vendor
Where keyVendor = #TempStore.keyVendor)
 
Select
  i.keyItem,  
  i.nameItem,
  i.othCategoryType,
  i.othDescription,
  i.othItemType,
  i.othItemValue,
  i.othMinorClassification,
  i.othMinorType,
  i.othMinorValue,
  i.sysUpc,  
  ts.othQuantity,
  ts.othPar,
  ts.keyVendor,
  ts.nameVendor  
From  #TempStore ts
      Inner Join Item i on ts.KeyItem = i.KeyItem
Order By ts.nameVendor, i.nameItem  
            
return 0

End Try

Begin Catch      
      return error_number()
End Catch
0
Comment
Question by:picsnet
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:sbagireddi
ID: 21827563
Can you post the code using the table variables?
0
 
LVL 39

Expert Comment

by:appari
ID: 21827569
where is your code using table variable?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21827626
You can't SELECT ...INTO a table variable, so you have to declare it instead:

declare @TempStore TABLE(
KeyItem INT,
othQuantity INT,
othPar varchar(20),
keyVendor INT,
nameVendor varchar(100)
)

INSERT INTO @TempStore(KeyItem, othQuantity, othPar, keyVendor, nameVendor)
Select
  i.keyItem,  
  sum(iv.othQuantity) as othQuantity,
  p.othPar,
  Cast(0 as int) as keyVendor,
  Cast('' as varchar(100)) as nameVendor
From Item i
    Left Join Inventory iv on iv.keyItem = i.keyItem and iv.keyLocation = @keyLocation
    Left Join Par p on i.keyItem = p.keyItem and p.keyLocation = @keyLocation
Where (i.keyLocation = @keyLocation      
       or (i.keyLocation is null and i.keyCompany = @keyCompany))
  and  i.keyChildItem < 1
  and  i.sysActive = 1  
  and  othQuantity <= p.othPar
Group By
 i.keyItem,
 p.othPar

From there, you can just swap out # for @....
0
 
LVL 1

Author Comment

by:picsnet
ID: 21827643
i don't have it in a table variable.  i deleted it cause it wasn't working.  And I know I can't do a sdlect into a table variable.  I had that changed.  

It was the update statements that weren't working
0
 
LVL 1

Author Comment

by:picsnet
ID: 21827658
Here is the translated...

ALTER PROCEDURE [dbo].[p_GetTotalInventoryByVendorPar]
      @keyCompany int,
      @keyLocation int      
as

set nocount on

Begin Try

Declare @TempStore Table(
  keyItem int,
  othQuantity decimal(18,5),
  othPar decimal(18,5),
  keyVendor int,
  nameVendor nvarchar(100))

Insert Into @Tempstore(
  keyItem,
  othQuantity,
  othPar,
  keyVendor,
  namevendor
)
Select
  i.keyItem,  
  sum(iv.othQuantity) as othQuantity,
  p.othPar,
  Cast(0 as int) as keyVendor,
  Cast('' as varchar(100)) as nameVendor
From Item i
    Left Join Inventory iv on iv.keyItem = i.keyItem and iv.keyLocation = @keyLocation
    Left Join Par p on i.keyItem = p.keyItem and p.keyLocation = @keyLocation
Where (i.keyLocation = @keyLocation      
       or (i.keyLocation is null and i.keyCompany = @keyCompany))
  and  i.keyChildItem < 1
  and  i.sysActive = 1  
  and  othQuantity <= p.othPar
Group By
 i.keyItem,
 p.othPar

Update @TempStore
Set keyVendor =
(Select top 1 keyVendor
From PurchaseOrder p
      Inner Join PurchaseOrderItem po on p.keyPurchaseOrder = po.keyPurchaseOrder
Where po.keyItem = @TempStore.KeyItem
and   p.keyLocation = @keyLocation
Order by p.keyPurchaseOrder Desc)

Update @TempStore
Set nameVendor =
(Select nameVendor
From Vendor
Where keyVendor = @TempStore.keyVendor)
 
Select
  i.keyItem,  
  i.nameItem,
  i.othCategoryType,
  i.othDescription,
  i.othItemType,
  i.othItemValue,
  i.othMinorClassification,
  i.othMinorType,
  i.othMinorValue,
  i.sysUpc,  
  ts.othQuantity,
  ts.othPar,
  ts.keyVendor,
  ts.nameVendor  
From  @TempStore ts
      Inner Join Item i on ts.KeyItem = i.KeyItem
Order By ts.nameVendor, i.nameItem  
            
return 0

End Try

Begin Catch      
      return error_number()
End Catch


Errors:
Msg 137, Level 15, State 2, Procedure p_GetTotalInventoryByVendorPar, Line 48
Must declare the scalar variable "@TempStore".
Msg 137, Level 15, State 2, Procedure p_GetTotalInventoryByVendorPar, Line 56
Must declare the scalar variable "@TempStore".



0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 21827890
try changing your update statements to

Update temp
Set keyVendor =
(Select top 1 keyVendor
From PurchaseOrder p
      Inner Join PurchaseOrderItem po on p.keyPurchaseOrder = po.keyPurchaseOrder
Where po.keyItem = temp.KeyItem
and   p.keyLocation = @keyLocation
Order by p.keyPurchaseOrder Desc)
from @TempStore temp

Update temp
Set nameVendor =
(Select nameVendor
From Vendor
Where keyVendor = temp.keyVendor)
from @TempStore temp
0
 
LVL 1

Author Comment

by:picsnet
ID: 21828185
yep that was it... i can never remember, and hate when it forces you to do stuff like this...

Tony
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21828189
try this:
ALTER PROCEDURE [dbo].[p_GetTotalInventoryByVendorPar]

      @keyCompany int,

      @keyLocation int      

as
 

set nocount on
 

Begin Try
 

Declare @TempStore Table(

  keyItem int,

  othQuantity decimal(18,5),

  othPar decimal(18,5),

  keyVendor int,

  nameVendor nvarchar(100))
 

Insert Into @Tempstore(

  keyItem,

  othQuantity,

  othPar,

  keyVendor,

  namevendor

)

Select 

  i.keyItem,  

  sum(iv.othQuantity) as othQuantity,

  p.othPar,

  Cast(0 as int) as keyVendor,

  Cast('' as varchar(100)) as nameVendor 

From Item i

    Left Join Inventory iv on iv.keyItem = i.keyItem and iv.keyLocation = @keyLocation

    Left Join Par p on i.keyItem = p.keyItem and p.keyLocation = @keyLocation

Where (i.keyLocation = @keyLocation      

       or (i.keyLocation is null and i.keyCompany = @keyCompany))

  and  i.keyChildItem < 1

  and  i.sysActive = 1  

  and  othQuantity <= p.othPar 

Group By 

 i.keyItem,

 p.othPar
 

Update t

Set keyVendor = 

(Select top 1 keyVendor 

From PurchaseOrder p

      Inner Join PurchaseOrderItem po on p.keyPurchaseOrder = po.keyPurchaseOrder

Where po.keyItem = t.KeyItem

and   p.keyLocation = @keyLocation

Order by p.keyPurchaseOrder Desc)

from @TempStore t
 

Update t

Set nameVendor = 

(Select nameVendor

From Vendor

Where keyVendor = t.keyVendor) 

from @TempStore t

Select 

  i.keyItem,  

  i.nameItem,

  i.othCategoryType,

  i.othDescription,

  i.othItemType,

  i.othItemValue,

  i.othMinorClassification,

  i.othMinorType,

  i.othMinorValue,

  i.sysUpc,  

  ts.othQuantity,

  ts.othPar,

  ts.keyVendor,

  ts.nameVendor   

From  @TempStore ts

      Inner Join Item i on ts.KeyItem = i.KeyItem

Order By ts.nameVendor, i.nameItem  

            

return 0
 

End Try
 

Begin Catch      

      return error_number()

End Catch 

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21828196
No points for this?  I answered your origina question....
0
 
LVL 1

Author Comment

by:picsnet
ID: 21828411
the hek you did... you only told me what i already knew... you didn't even look at the update statements.  

0
 
LVL 1

Author Comment

by:picsnet
ID: 21828419
well you changed the update statements in your last post, but you'll notice that it wsa after the question was already finished...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now