Dynamically storing values in SQL table - sql

I'm trying to dynamically create a variable or table in SQL which will store distinct values as a result for another sql query.
declare sample_table table
( values varchar(100))
insert into #sample_table values (select t1.value from my_tablw as t1 group by t1.value);
Supposing the distinct values in column value can change from table query to another table query, I want to store the result of this query in a user defined variable/table which can be used later in another query.

Depending on your definition of can be used later you can use a local temp table or table variable.... you just need to change the syntax a bit to not use the values since you are inserting from the results of a query. I also used DISTINCT below which is clearer than the GROUP BY without an aggregate function.
declare sample_table table ([values] varchar(100))
insert into #sample_table
select distinct t1.value
from my_tablw as t1
--one way to use it
select *
from newTable
where columnVal in (select * from #sample_table)
--another way to use it
select at.*
from anotherTable at
inner join #sample_table t on
t.column = at.column
--and another way...
select f.*
from finalTable f
where exists (select * from #sample_table t where t.column = f.column)
If you need this to be used outside the scope of your current batch, you'll need to use a persisted table or global temporary table.

Related

How to insert data from CTE to a Temp Table?

I am trying to create a some logic using CTE and then instead of using DML statement after CTE, I am trying to create a temp table using CTE. This is possible in T-SQL. Is it possible in GBQ?
I know I can create temp table instead of CTE in the below example, but just want to know the possibility!
WITH xyz AS
(SELECT * FROM table1)
CREATE TEMP TABLE temp1 AS (
SELECT * FROM xyz INNER JOIN table2 on ...);
Use below instead
CREATE TEMP TABLE temp1 AS (
WITH xyz AS
(SELECT * FROM table1)
SELECT * FROM xyz INNER JOIN table2 on ...
);
So in 2022 I believe that no longer works without a script or session in GBQ:
You could write your query as follows:
WITH xyz AS (
SELECT
*
FROM table1
)
SELECT
*
FROM xyz
INNER JOIN table2
ON ...
and then click the More Button -> Query Settings as shown below:
After that you can set a destination for your results a a temporary table and here you can define the name of your table etc. in your case it's temp1:
This way you can just save the results of your query into a temporary table. Hope it helps!

SQL select from column where column equals table variable?

I'm a serious SQL noob so any help is appreciated. I'm having a hard time even explaining what I'm trying to do so I'll lay out what I have so far:
DECLARE #UserIDInt table (ID int);
INSERT into #UserIDInt
SELECT UserId
FROM [LcsCDR].[dbo].[Users]
WHERE [LcsCDR].[dbo].[Users].[UserUri] LIKE '%example';
SELECT *
FROM [LcsCDR].[dbo].[SessionDetails]
WHERE [LcsCDR].[dbo].[SessionDetails].[User1Id] = #UserIDInt;
"DECLARE #UserIDInt table (ID int);"
This creates my variable with a column called "ID"
INSERT into #UserIDInt
SELECT UserId
FROM [LcsCDR].[dbo].[Users]
WHERE [LcsCDR].[dbo].[Users].[UserUri] LIKE '%example';
This adds numeric values into the ID column based on whether or not the WHERE statement matched
SELECT *
FROM [LcsCDR].[dbo].[SessionDetails]
WHERE [LcsCDR].[dbo].[SessionDetails].[User1Id] = #UserIDInt;
This is where I am lost. I am trying to return all rows from [LcsCDR].[dbo].[SessionDetails] if the column [LcsCDR].[dbo].[SessionDetails].[User1Id] matches anything in my variable. The problem (I think) I'm having is that SQL can't look within the variable's column to find multiple values. Basically, the ID column in my variable #UserIDInt will contain a bunch of numeric values.
How do I perform the final SELECT statement and have SQL return all results if [LcsCDR].[dbo].[SessionDetails].[User1Id] matches anything within my #UserIDInt.ID column?
I am using SQL Server 2014.
Apologies if I explained it badly. Not sure how else to ask the question :)
using inner join:
select sd.*
from [lcscdr].[dbo].[sessiondetails] sd
inner join #useridint i
on i.id = sd.user1id;
or using exists():
select sd.*
from [lcscdr].[dbo].[sessiondetails] sd
where exists (
select 1
from #useridint i
where i.id = sd.user1id
);
or using in():
select sd.*
from [lcscdr].[dbo].[sessiondetails] sd
where sd.user1id in (
select id
from #useridint i
);
rextester demo: http://rextester.com/UVCB28056
Use EXISTS:
SELECT T1.*
FROM [lcscdr].[dbo].[sessiondetails] T1 WHERE EXISTS (
SELECT 1
FROM #useridint T2
WHERE T2.id = T1.user1id
);

SQL update statement with different table in from clause

Out of accident I noticed that the following query is actually valid:
UPDATE bikes
SET price = NULL
FROM inserted
WHERE inserted.owner_id = 123456
This is part of a trigger where someone forgot to join the original table to the inserted table. The result is that when the trigger is executed, all prices are set to NULL.
The correct SQL statement is this:
UPDATE bikes
SET price = NULL
FROM inserted
INNER JOIN bikes ON bikes.id=inserted.id
WHERE inserted.owner_id = 123456
How/why is this first statement valid?
Why wouldn't it be valid? SQL Server doesn't know what you're trying to do. It thinks you want to update all of the fields where some condition exists on another table. See the last update below.
SETUP
declare #table table
(
id int,
name varchar(10)
)
declare #itable table
(
id int,
name varchar(10)
)
insert into #table (id, name)
select 1,'abc' union
select 2,'def' union
select 3,'ghi' union
select 4,'jkl' union
select 5,'mno' union
select 6,'pqr'
insert into #itable (id, name)
select 1,'abc' union
select 2,'def' union
select 3,'ghi' union
select 4,'jkl' union
select 5,'mno' union
select 6,'pqr'
All names on #table will change to zzz
update #table
set name = 'zzz'
from #itable i
where i.id = 1
select * from #itable
select * from #table
All names where id = 1 on #table becomes yyy
update #table
set name = 'yyy'
from #itable i
inner join #table t on i.id = t.id
where i.id = 1
select * from #itable
select * from #table
This will NOT update anything
update #table
set name = 'aaa'
from #itable i
where i.id = 133
select * from #itable
select * from #table
The first statement does not work as expected because it is missing the entire INNER JOIN line with the bikes and inserted table. Without that SQL Server will update all rows as all rows will qualify for an update when the inserted.owner_id = 123456.
You can reproduce this outside of the trigger in TSQL like :
update bikes set price =null
from SomeOtherTable
where SomeOtherTable.SomeColumn = 'some_value_that_exists'
This is syntactically valid statement in SQL Server. If the Intention is to update bikes table based on existance of a row in some unrelated table that cant be joined because the 2 tables arent related then this is how you would do it. But that is not your requirement. Hence why it updates all records instead of only those that match the bikes.id In programming terms this is called as a logical bug.
The inner join makes it more restrictive and forces to to update only those rows that match the join condition between the 2 tables (bikes.id=inserted.id comparison )
In Simple terms the from clause is optional..Consider below query..
update table
set id=10
This has one table right after update clause ,sql just updates it..
now consider below query..
update table1
set id=40
from table2..
What do you think SQL does,it updates all the rows same as first query..
unless you refer to another table in from clause and join like below
update t1
set t1.id=40
from
table1 t1
join
table2 t2
on t1.id=t2.id
below is the from clause explanation in update syntax stripped down to show only to relevant parts..
If the object being updated is the same as the object in the FROM clause and there is only one reference to the object in the FROM clause, an object alias may or may not be specified. If the object being updated appears more than one time in the FROM clause, one, and only one, reference to the object must not specify a table alias. All other references to the object in the FROM clause must include an object alias
As long as above rules are valid (as in your case),SQL will happily update table found immediately after update clause

Select Into error

SELECT
xfqti_virtuemart_products_pt_pt.virtuemart_product_id,
xfqti_virtuemart_product_medias.virtuemart_media_id
INTO #tempTable
FROM xfqti_virtuemart_products_pt_pt
Gives syntax error, I'm about to pull my hair off
Being Virtuemart, I'm guessing this is a MySQL database. If so, the correct syntax for creating a temp table is:
CREATE TEMPORARY TABLE IF NOT EXISTS tempTableName AS
(
SELECT field1, field2
FROM yourtable;
)
That being said, your SELECT statement has two fields from two different tables, but only one of those tables is mentioned in the FROM clause of your statement. They should really both be in there and JOINed. Something like:
CREATE TEMPORARY TABLE IF NOT EXISTS tempTableName AS
(
SELECT
t1.virtuemart_product_id,
t2.virtuemart_media_id
FROM
xfqti_virtuemart_products_pt_pt as t1
INNER JOIN xfqti_virtuemart_product_medias as t2 ON
t1.product_id = t2.product_id
)
Or something.. I can't see your tables and it's been years since I used Virtuemart, so it's just a guess at the table relationship.
Insert Into and Selecthave these sintax
Insert into your_Table (col1,col2)
SELECT
xfqti_virtuemart_products_pt_pt.virtuemart_product_id,
xfqti_virtuemart_product_medias.virtuemart_media_id
FROM xfqti_virtuemart_products_pt_pt
for create table
Create your_Table as
SELECT
xfqti_virtuemart_products_pt_pt.virtuemart_product_id,
xfqti_virtuemart_product_medias.virtuemart_media_id
FROM xfqti_virtuemart_products_pt_pt

SQL query results into a table

I wanna put the results from sql query into a table but if i do SELECT columns names INTO NEWTABLE(which is not created), its good for one time but when i run that query again, it says table already exists. All i am doing is results from that query into the newtable and i am gonna run it every week so it should not give me the error: table already exists.
For Example : i wanna put Following query results into a Newtable EmployeeDetails and i am gonna run it every week.
select a.Name, b.Id
from Database1 a left join
Database2 b
ON a.Id = b.Id
Use INSERT INTO instead.
But make sure that the table is already created before you call insert into the first time,
INSERT INTO table1 (column1, column2, ...)
SELECT column3, column4, ...
FROM table2
Check with IF EXISTS
example for sql server
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[YOurTableName]')
AND type in (N'U'))
BEGIN
DROP TABLE [dbo].[YOurTableName
END
select * into [dbo].[YOurTableName]
from ......
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[YOurTableName]') AND type in (N'U'))
//INSERT INTO STATEMENT
ELSE
//SELECT INTO STATEMENT THAT U ALREADY HAVE
use insert -
insert into newtbl select x,y,z from tbl
(make sure you create the table first)
If you only need the generated table for the duration of the current SQL connection, create a temporary table:
CREATE TEMPORARY TABLE newtable SELECT the_rest_of_your_query
Or, if you want to keep the table around for a week, but replace it completely each week, make sure it's gone first:
DROP TABLE IF EXISTS newtable