[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Trying to translate to use a table variable instead

Posted on 2008-06-19
11
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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
 
LVL 39

Accepted Solution

by:
appari earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

650 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