| Author |
Topic |
|
Anushka
Yak Posting Veteran
79 Posts |
Posted - 2008-06-18 : 09:45:47
|
| HI,This is my req...i have to sum up the qty n that qty should be added to qtyonhand in other table....Declare Qty INTQty =SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemIDUPDATE IWSET IW.QtyOnHand=IW.QtyOnHand + Qty - @TAllotedQtyFROM InventoryByWarehouse IWwhere IW.CompanyID = @CompanyIDAND IW.DivisionID = @DivisionID AND IW.DepartmentID = @DepartmentIDAND IW.ItemID=@ItemIDThanks... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 09:52:53
|
quote: Originally posted by Anushka HI,This is my req...i have to sum up the qty n that qty should be added to qtyonhand in other table....Declare @Qty INTSET @Qty =SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemIDUPDATE IWSET IW.QtyOnHand=IW.QtyOnHand + Qty - @TAllotedQtyFROM InventoryByWarehouse IWwhere IW.CompanyID = @CompanyIDAND IW.DivisionID = @DivisionID AND IW.DepartmentID = @DepartmentIDAND IW.ItemID=@ItemIDThanks...
You should put @ for variable names |
 |
|
|
Anushka
Yak Posting Veteran
79 Posts |
Posted - 2008-06-18 : 10:15:54
|
| Incorrect syntax near select SET @Qty =SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemIDwe have to keep SET @Qty ='SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemID'itemid is parameter so is it I tried like declare @ItemID NVARCHAR(36)set @ItemID='bbbbbbbb'Declare @Qty INTSET @Qty = 'SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID ='+@ItemID+'' PRINT @Qtybut error:Conversion failed when converting the nvarchar value 'SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID =bbbbbbbb' to data type int. |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-06-18 : 10:20:11
|
| SET @Qty =(SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemID)orselect @Qty = SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemIDEm |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-18 : 10:20:37
|
| UseSELECT @Qty = SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemIDMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-18 : 10:21:39
|
MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 10:23:38
|
quote: Originally posted by Anushka Incorrect syntax near select SET @Qty =SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemIDwe have to keep SET @Qty ='SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID=@ItemID'itemid is parameter so is it I tried like declare @ItemID NVARCHAR(36)set @ItemID='bbbbbbbb'DECLARE @Sql varchar(8000)SET @Sql = 'Declare @Qty INT;SELECT @Qty =SUM(Quantity) FROM InventoryLedger WHERE ITEMID ='+@ItemID EXEC(@Sql)PRINT (@Qty)but error:Conversion failed when converting the nvarchar value 'SELECT SUM(Quantity) FROM InventoryLedger WHERE ITEMID =bbbbbbbb' to data type int.
Change like above and try |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 10:32:59
|
Wat about this?declare @ItemID NVARCHAR(36)set @ItemID='bbbbbbbb'DECLARE @Sql varchar(8000)Declare @Qty INT;SET @Sql = 'SELECT @Qty =SUM(Quantity) FROM InventoryLedger WHERE ITEMID ='+@ItemID EXEC(@Sql)PRINT (@Qty) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-18 : 10:34:25
|
>>Must declare the scalar variable "@Qty".As it says you need to declare it MadhivananFailing to plan is Planning to fail |
 |
|
|
Anushka
Yak Posting Veteran
79 Posts |
Posted - 2008-06-18 : 10:35:56
|
| yes it is sayingMust declare the scalar variable "@Qty". |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-18 : 10:37:35
|
quote: Originally posted by madhivanan >>Must declare the scalar variable "@Qty".As it says you need to declare it MadhivananFailing to plan is Planning to fail
It seems OP deleted the reply. My reply was to the OP MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 10:37:57
|
quote: Originally posted by Anushka yes it is sayingMust declare the scalar variable "@Qty".
I think that was because variable was declared inside the dynamic string. Try taking declare statement out as in last post. |
 |
|
|
Anushka
Yak Posting Veteran
79 Posts |
Posted - 2008-06-18 : 10:56:52
|
| declare @ItemID NVARCHAR(36)set @ItemID='bbbbbbbb'DECLARE @Sql varchar(8000)Declare @Qty INT;SET @Sql = 'select SUM(Quantity) FROM InventoryLedger WHERE ITEMID ='+@ItemID EXEC(@Sql)PRINT (@Qty)invalid column name 'bbbbbbbb' |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-18 : 11:12:03
|
[code]DECLARE @Qty INTSELECT @Qty = SUM(Quantity)FROM InventoryLedgerWHERE ITEMID = @ItemIDUPDATE InventoryByWarehouseSET QtyOnHand = QtyOnHand + @Qty - @TAllotedQtyFROM InventoryByWarehouseWHERE CompanyID = @CompanyID AND DivisionID = @DivisionID AND DepartmentID = @DepartmentID AND ItemID = @ItemID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 11:18:09
|
quote: Originally posted by Anushka declare @ItemID NVARCHAR(36)set @ItemID='bbbbbbbb'DECLARE @Sql varchar(8000)Declare @Qty INT;SET @Sql = 'select SUM(Quantity) FROM InventoryLedger WHERE ITEMID ='+@ItemID EXEC(@Sql)PRINT (@Qty)invalid column name 'bbbbbbbb'
Any special reason why you used NVARCHAR for @itemID? |
 |
|
|
Anushka
Yak Posting Veteran
79 Posts |
Posted - 2008-06-19 : 01:08:11
|
| hi,iam getting null for qtyonhand by writing like this:actually:frm front end i will b getting values like thisselect Quantity FROM InventoryLedgerWHERE TransDate = '2008-06-19 00:00:00.000'so : Qty = 10select QtyOnHand FROM InventoryByWarehouseWHERE ItemID = 'bbbbbbbbbbbb'@TAllotedQty = 20QtyOnHand=300 +10 -20 =290DECLARE @Qty INTSELECT @Qty = QuantityFROM InventoryLedgerWHERE TransDate = @TrndateUPDATE InventoryByWarehouseSET QtyOnHand = QtyOnHand + @Qty - @TAllotedQtyFROM InventoryByWarehouseWHERE CompanyID = @CompanyID AND DivisionID = @DivisionID AND DepartmentID = @DepartmentID AND ItemID = @ItemIDthanx |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-19 : 01:20:04
|
SET QtyOnHand = QtyOnHand + coalesce(@Qty, 0) - @TAllotedQty E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Anushka
Yak Posting Veteran
79 Posts |
Posted - 2008-06-19 : 01:38:54
|
| Hi,wat is this coalesce(@Qty, 0) -by writing this ,i think qty it is taking as 0 instead of passing parameter(@qty)...i got the value QtyOnHand - @TAllotedQty by writing like this:SET QtyOnHand = QtyOnHand + coalesce(@Qty, 0) - @TAllotedQty |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-19 : 02:14:16
|
quote: Originally posted by Anushka Hi,wat is this coalesce(@Qty, 0) -by writing this ,i think qty it is taking as 0 instead of passing parameter(@qty)...i got the value QtyOnHand - @TAllotedQty by writing like this:SET QtyOnHand = QtyOnHand + coalesce(@Qty, 0) - @TAllotedQty
this will ensure it will take value 0 if @Qty is NULL or take the value in variable @Qty |
 |
|
|
Anushka
Yak Posting Veteran
79 Posts |
Posted - 2008-06-19 : 02:15:04
|
| Hey PEOS I got it ,Rechecked....but wat is this coalesce(@Qty, 0) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-19 : 02:21:06
|
quote: Originally posted by Anushka Hey PEOS I got it ,Rechecked....but wat is this coalesce(@Qty, 0)
Read about COALESCE in books online. It has good explanation about it. |
 |
|
|
Next Page
|