I have tried lots of permutations of DECLARE and CREATE but I can't display any data from my temporary table. Here's the latest iteration:
DECLARE GLOBAL TEMPORARY TABLE session.temporary_table (ACTNO CHAR);
INSERT INTO session.temporary_table VALUES ('1'), ('2');
SELECT * FROM session.temporary_table;
SELECT DISTINCT ACTKWD FROM JMILLER.ACT INNER JOIN session.temporary_table ON ACT.ACTNO = session.temporary_table.ACTNO;
DROP TABLE session.temporary_table;
The first SELECT is to see if there's anything in the temporary_table. Which there doesn't seem to be.
Even though when I test the query in this tool it says every line of it completed correctly.
What am I doing wrong in my sql statements? I was having trouble with permissions earlier but that seems to be fine now and I'm not getting any errors, so it must be in my code.
You did not add the clause ON COMMIT PRESERVE ROWS.
Also see if you could make things simpler by using CTE (the WITH statement)
Modify to
SELECT DISTINCT ACTKWD FROM JMILLER.ACT t0 INNER JOIN session.temporary_table t1 ON t0.ACTNO = t1.ACTNO;
Related
I have a complex stored procedure that will also call other stored procedures as part of the workflow. I have checked all stored procedures for the ambiguous column 'ColumnId' error.
The first oddity is that the error is paramaterized with a single input and the error will not recreate for all users even with the same input. The second oddity is that I have checked all the SELECT, JOIN, WHERE, ORDER BY, and GROUP BY for the normal errors of ambiguity and not found any violations.
The only potential violation might be
SELECT RateID
FROM Rate.tblRate
INNER JOIN #tmpRate
ON tblRate.CustomerID = #tmpRate.CustomerID
Could the line for ON be an issue as it is not
ON Rate.tblRate.CustomerID = #tmpRate.CustomerID
In your case, the proc could return different, or multiple result sets making this behavior sporadic. However,I've seen this for temp tables a lot, though I can't explain why. If you alias that table, it resolves it every time.
SELECT RateID
FROM Rate.tblRate r
INNER JOIN #tmpRate t
ON r.CustomerID = t.CustomerID
This is good practice as it is required for other instances, like table variables.
if object_id('tempdb..#temp') is not null drop table #temp
select 1 as ID into #temp
declare #table table (ID int)
insert into #table
values
(1)
select *
from
#table
inner join #temp on #temp.ID = #table.ID
This will throw the error:
Must declare the scalar variable "#table".
So, alias it and it will work:
select *
from
#table t
inner join #temp on #temp.ID = t.ID
There are a lot of blogs out there on why it's a good habit to pick up. Here is one.
I have a table that contains several repair categories, and items that are associated with each repair category. I am trying to insert all the standard items from a specific repair category that don't already exist into a Details table.
TblEstimateDetails is a join table for an Estimate Table and StandardItem Table. And TblCategoryItems is a join table for the Repair Categories and their respective Standard Items.
For example in the attached image, Left side are all the Standard Items in a Repair Category, and Right side are all the Standard Items that are already in the EstimateDetails table.
Standard Items All vs Already Included
I need to be able to insert the 6 missing GUIDS from the left, and into the table on the right, and only for a specific estimate GID.
This is being used in an Access VBA script, which I will translate into the appropriate code once I get the sql syntax correct.
Thank you!
INSERT INTO TblEstimateDetails(estimate_GID, standard_item_GID)
SELECT
'55DEEE29-7B79-4830-909C-E59E831F4297' AS estimate_GID
, standard_item_GID
FROM TblCategoryItems
WHERE repair_category_GID = '32A8AE6D-A512-4868-8E1A-EF0357AB100E'
AND NOT EXISTS
(SELECT standard_item_GID
FROM TblEstimateDetails
WHERE estimate_GID = '55DEEE29-7B79-4830-909C-E59E831F4297');
Some things to try: 1) simplify to a select query to see if it selects the right records, 2) use a NOT IN statement instead of NOT EXISTS. There's no reason NOT EXISTS shouldn't work, I'd just try a different way if it isn't working.
SELECT '55DEEE29-7B79-4830-909C-E59E831F4297' AS estimate_GID,
standard_item_GID
FROM TblCategoryItems
WHERE repair_category_GID = '32A8AE6D-A512-4868-8E1A-EF0357AB100E'
AND standard_item_GID NOT IN
(SELECT standard_item_GID FROM TblEstimateDetails
WHERE estimate_GID = '55DEEE29-7B79-4830-909C-E59E831F4297');
Got it figured out. Access needs the subquery to be correlated to main query to work. So I set the WHERE clause in the subquery to equal the matching column in the main query. And I had to join the Estimates table so that it picked only the items in a specific estimate.
SELECT
'06A2E0A9-9AE5-4073-A856-1CCE6D9C48BB' AS estimate_GID
, CI.standard_item_GID
FROM TblCategoryItems CI
INNER JOIN TblEstimates E ON CI.repair_category_GID=E.repair_category_GID
WHERE E.repair_category_GID = '15238097-305E-4456-B86F-3787C9B8219B'
AND NOT EXISTS
(SELECT ED.standard_item_GID
FROM TblEstimateDetails ED
WHERE E.estimate_GID=ED.estimate_GID
);
I currently have a performance issue with a query (that is more complicated than the example below). Originally the query would run and take say 30 seconds, then when I switched out the use of a table variable to using a temp table instead, the speed is cut down to a few seconds.
Here is a trimmed down version using a table variable:
-- Store XML into tables for use in query
DECLARE #tCodes TABLE([Code] VARCHAR(100))
INSERT INTO
#tCodes
SELECT
ParamValues.ID.value('.','VARCHAR(100)') AS 'Code'
FROM
#xmlCodes.nodes('/ArrayOfString/string') AS ParamValues(ID)
SELECT
'SummedValue' = SUM(ot.[Value])
FROM
[SomeTable] st (NOLOCK)
JOIN
[OtherTable] ot (NOLOCK)
ON ot.[SomeTableID] = st.[ID]
WHERE
ot.[CodeID] IN (SELECT [Code] FROM #tCodes) AND
st.[Status] = 'ACTIVE' AND
YEAR(ot.[SomeDate]) = 2013 AND
LEFT(st.[Identifier], 11) = #sIdentifier
Here is the version with the temp table which performs MUCH faster:
SELECT
ParamValues.ID.value('.','VARCHAR(100)') AS 'Code'
INTO
#tCodes
FROM
#xmlCodes.nodes('/ArrayOfString/string') AS ParamValues(ID)
SELECT
'SummedValue' = SUM(ot.[Value])
FROM
[SomeTable] st (NOLOCK)
JOIN
[OtherTable] ot (NOLOCK)
ON ot.[SomeTableID] = st.[ID]
WHERE
ot.[CodeID] IN (SELECT [Code] FROM #tCodes) AND
st.[Status] = 'ACTIVE' AND
YEAR(ot.[SomeDate]) = 2013 AND
LEFT(st.[Identifier], 11) = #sIdentifier
The problem I have with performance is solved with the change but I just don't understand why it fixes the issue and would prefer to know why. It could be related to something else in the query but all I have changed in the stored proc (which is much more complicated) is to switch from using a table variable to using a temp table. Any thoughts?
The differences and similarities between table variables and #temp tables are looked at in depth in my answer here.
Regarding the two queries you have shown (unindexed table variable vs unindexed temp table) three possibilities spring to mind.
INSERT ... SELECT to table variables is always serial. The SELECT can be parallelised for temp tables.
Temp tables can have column statistics histograms auto created for them.
Usually the cardinality of table variables is assumed to be 0 (when they are compiled when the table is empty)
From the code you have shown (3) seems the most likely explanation.
This can be resolved by using OPTION (RECOMPILE) to recompile the statement after the table variable has been populated.
Hi i got a little problem right now i'm doing Stored Proc in sql server. I wanna get some result from multiple table and put it into a temporary table. So I thought i could use a "Select into" statement wich work fine until i decided to add multiple select into the temporary table. For example: Here's my code:
while #Compteur <= #wrhCodeEnd
select lp.lotQty ,lp.lotID into ZeTable from TB_lotLot ltlt
inner join TB_lotPal lp on ltlt.lotID=lp.lotID
inner join TB_palSuper ps ON lp.spID=ps.spID
inner join TB_expExpeditionLot eel ON eel.lotID=lp.spID
where ps.wrhID <> #Compteur and
ltlt.wrhID = #Compteur and
lp.lotID not in(select ZeTable.lotID from ZeTable)
the thing is I don't know if I can make multiple select into on the same temporary table and I also wanna check with a where clause the information in the table is not already there.
Thx in Advance
You can create temporary table and can use insert statement to add records with required columns and can drop it after use.
I am using the following code, to combine several tables into 1 single Table called DB_Total:
INSERT INTO DB_Total
SELECT *
FROM Tags_DI_DB;
But, if I (accidentally) execute this query twice or three times, the data just gets stacked. Is there a possibility to check wether or not a table is added in the new "mastertable", DB_Total?
INSERT INTO DB_TOTAL
SELECT a.*
FROM Tags_DI_DB a
LEFT JOIN DB_TOTAL b
ON a.colName = b.colName
WHERE b.colName IS NULL
where colName is the unique column