Solved

Trying to translate to use a table variable instead

Posted on 2008-06-19
11
196 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

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

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.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

738 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