Solved

Trying to translate to use a table variable instead

Posted on 2008-06-19
11
194 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

809 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