Must declare the scalar variable #tempTbl - sql

I get this error even though i have tried what other post suggested still the same error
but when i run select it works OK...
any help would be greatly appreciated
ALTER PROCEDURE UpdateCustomers
#XML AS XML
AS
DECLARE #tempTbl TABLE(
tblID INT ,
Customer_name NVARCHAR(30),
Customer_Code NVARCHAR(10)
)
INSERT INTO #tempTbl(tblID, Customer_name, Customer_Code)
SELECT
Item.element.value('#tblID', 'int'),
Item.element.value('#Customer_name', 'nvarchar(30)'),
Item.element.value('#Customer_Code', 'nvarchar(10)')
FROM
#xml.nodes('/root/item') AS Item(element)
--SELECT * FROM #tempTbl---it runs ok
UPDATE dbo.Customers
SET
dbo.Customers.Customer_name = #tempTbl.Customer_name,
dbo.Customers.Customer_Code = #tempTbl.Customer_Code
from dbo.Customers
inner join #tempTbl
on Customers.tblID =#tempTbl.tblID

Try aliasing the table variable in the UPDATE
e.g.
UPDATE dbo.Customers
SET
dbo.Customers.Customer_name = tmp.Customer_name,
dbo.Customers.Customer_Code = tmp.Customer_Code
from dbo.Customers
inner join #tempTbl tmp
on Customers.tblID = tmp.tblID

Related

Temp table empty after sql insert exec

Very confused as to why the final select * from #tempTbl is empty.
When I run this, everything else works fine, but I need the results in a temp table to continue with the next step of the stored procedure. I've been searching every forum possible including this one, and cannot fix this issue. Any help would be greatly appreciated.
CREATE TABLE #tempTbl
(
POID VARCHAR(15),
UPC VARCHAR(15),
QtyOrdered int,
DateOrdered date,
Closed bit
)
insert into #tempTbl (POID, UPC, QtyOrdered, DateOrdered, Closed)
exec dbo.sp_executesql #as4sql;
--exec (#as4sql)
select * from #tempTbl
Update:
Here's my set:
set #as4sql = 'INSERT INTO tblPODetail
(POID, UPC, QtyOrdered, DateOrdered, Closed)
(SELECT pod.PONumber, pod.UPC, pod.QtyOrdered, 1, 0
FROM [awx].[dbo].[POLines] pol
JOIN [awx].[dbo].[PODetails] pod
ON pol.PONumber = pod.PONumber
LEFT JOIN tblPODetail p
ON p.POID = pod.PONumber
WHERE p.POID IS NULL...

My query is not working as expected

My query is not working which is as follows:
CREATE TABLE #tempCal (
CategoryId BIGINT
,CategoryName NVARCHAR(max)
,ElementWeight MONEY
,MakingCharges MONEY
,GemstoneAttribute NVARCHAR(max)
,AlloyAttribute NVARCHAR(max)
,Rates MONEY
)
--insert into #tempCal(CategoryId,CategoryName,ElementWeight,MakingCharges,GemstoneAttribute,AlloyAttribute,Rates)
--values
DECLARE #iterator BIGINT = (
SELECT max(MstJewelleryProduct.ID)
FROM MstJewelleryProduct
)
INSERT INTO #tempCal (
CategoryId
,CategoryName
,ElementWeight
,MakingCharges
,GemstoneAttribute
,AlloyAttribute
,Rates
)
VALUES (
(
SELECT MstJewelleryProduct.ElementWeight
,MstJewelleryProduct.Element_Price
,MstJewelleryProduct.MakingCharges
,MstJewelleryProduct.GemstoneAttribute
,MstJewelleryProduct.AlloyAttribute
,MstJewelleryCategory.ID
,MstJewelleryCategory.CategoryName
,MstRates.Rates
,MstJewelleryOffers.OfferAmount
FROM MstJewelleryProduct
INNER JOIN MstJewelleryCategory ON MstJewelleryProduct.CategoryID = MstJewelleryCategory.ID
LEFT JOIN MstRates ON MstJewelleryProduct.CategoryID = MstRates.CategoryId
LEFT JOIN MstJewelleryOffers ON MstJewelleryProduct.CategoryID = MstJewelleryOffers.ProductCategory
AND MstJewelleryOffers.IsActive = 1
WHERE MstJewelleryProduct.IsActive = 1
)
)
SELECT *
FROM #tempCal
DROP TABLE #tempCal
The syntax of the (Insert Into) is not correct. When you use (Select) you do not need to use (Values). Please refer to this link to check the right syntax.

Dynamic sql using table variable -TSQL

My problem is using a table variable in a exec.
declare #sort_col nvarchar(1000) = 'itm_id'
declare #sort_dir nvarchar(4) = 'desc'
declare #filters nvarchar(1000) = ' and itm_name like ''%aa%'''
declare #temp table
(
itm_id int
)
insert into #temp
EXEC('select itm_id from Tblitm where itm_name not like ''%aa%''')
EXEC('select * from (select (ROW_NUMBER() OVER (ORDER BY '+#sort_col+' '+#sort_dir+')) row_num, * FROM (select itm_id, itm_name,
dbo.fnItmsHistory(itm_id) itm_history
from dbo.Tblitm as itm
left outer join '+#temp+' as temp on itm.itm_id = temp.itm_id
where itm_id=itm_id and temp.itm_id = null '+#filters+') as x) as tmp')
It says Must declare the scalar variable "#temp" when the temp table is declared i tried using original temp table and it worked, but i had problems when trying to update my entity model.So is there any solution for this problem?
Note:
I must use exec because in filters i store string for the where clause.
Try moving the table variable inside the dynamic statement.
EXEC('
declare #temp table
(
itm_id int
)
insert into #temp
select itm_id from Tblitm where itm_name not like ''%aa%''
select * from (select (ROW_NUMBER() OVER (ORDER BY '+#sort_col+' '+#sort_dir+')) row_num, * FROM (select itm_id, itm_name,
dbo.fnItmsHistory(itm_id) itm_history
from dbo.Tblitm as itm
left outer join #temp as temp on itm.itm_id = temp.itm_id
where itm_id=itm_id and temp.itm_id = null '+#filters+') as x) as tmp')
For solution i had to use a temp table and then on the start of my stored procedure i used the if condition from the EF can't infer return schema from Stored Procedure selecting from a #temp table anwser.
It's the best solution for this scenario i think.

UPDATE Select Statement with Multiple Joins

I am trying to update data in a Contacts_CSTM table based on data in a Project_CSTM table. This is the query I'm using, but I get an error: "Conversion failed when converting from a character string to uniqueidentifier"
ALTER PROCEDURE Insurance_Check_Expiration
#ID_C AS NVARCHAR (55) = ID_C
AS
BEGIN
SET NOCOUNT ON
IF EXISTS(SELECT * FROM CONTACTS_CSTM WHERE ID_C = #ID_c)
Update contacts_cstm set insurance_expired_label_c = 'INSURANCE EXPIRED'
WHERE DRIVERS_LICENSE_NUMBER_C IS NOT NULL AND #ID_C=
(SELECT cc.id_c
FROM PROJECT_CSTM PC
JOIN PROJECT P
ON P.ID = PC.ID_C
JOIN PROJECT_RELATION PR
ON PR.PROJECT_ID = P.ID
JOIN CONTACTS C
ON C.ID = PR.RELATION_ID
JOIN CONTACTS_CSTM CC
ON CC.ID_C = C.ID
WHERE CC.ID_C = #ID_C AND INSURANCE_EXPIRED_C ='1')
Thanks.
For one, you're setting the value of #ID_C to a value (ID_C) which is obviously not a valid GUID.
This is the functional equivalent of what you did, run it and you'll get the same error.
CREATE PROCEDURE Insurance_Check_Expiration
#ID_C AS NVARCHAR (55) = ID_C
AS
BEGIN
DECLARE #A UNIQUEIDENTIFIER
SET #A = #ID_C
END
exec Insurance_Check_Expiration
EDIT: Here's a functional example based on the OP's comments:
CREATE TABLE GUIDExample (ID_C UNIQUEIDENTIFIER)
GO
CREATE PROC GuidExample_Insert #ID_C UNIQUEIDENTIFIER
AS
BEGIN
SELECT #ID_C
END
GO
CREATE TRIGGER GUID_Example ON GUIDExample
AFTER INSERT
AS
DECLARE #ID_C UNIQUEIDENTIFIER
SELECT #ID_C = ID_C
FROM Inserted
EXEC GuidExample_Insert #ID_C
GO
DECLARE #SampleGUID UNIQUEIDENTIFIER
SET #SampleGUID = NEWID()
INSERT GUIDExample (ID_C)
VALUES(#SampleGUID)

Usage of Array in Sql Possible?

I am trying to write a stored proicedure that gets all the townID's from Region_TownPage table. Then i should get the City, Stateinitials of all the townID's.
Alter PROCEDURE [dbo].[GetTownDetailsforRegionID]
#RegionID int
AS
BEGIN
Declare #townID int
set #townID = (Select townID from Region_TownPage where regionID =#RegionID)
SET NOCOUNT ON;
Select City, StateInitials,TownID from TownPage where TownID =#townID
END
I do not know how to use an array here in sql. If someone could help me doing this, i really appreciate that.
Thanks in advance!!
I don't think you need an array - you just need to join the tables?
Select r.RegionId,
t.TownId,
t.City,
t.StateInitials
From Region_TownPage r
Join TownPage t on r.TownId = t.TownId
Where r.RegionId = #RegionId
you would declare a table variable instead of an int. so it would be something like
DECLARE #tab table(townID int)
INSERT INTO #tab
SELECT townID from Region_TownPage WHERE regionID = #RegionID
Select * From TownPage WHERE TownID IN(SELECT townID FROM #tab)