Solved

Trying to translate to use a table variable instead

Posted on 2008-06-19
11
190 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

911 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

24 Experts available now in Live!

Get 1:1 Help Now