Populating temporary table with result of independent Sql Query - sql

I want to return a temporary table from stored procedure which populates with data retrieved from two independent sql query
Select column1,column2 FROM TABLE1 WHERE someCondition
Select column3,column4 FROM TABLE1 WHERE someOtherCondition
INSERT INTO Temp_table(column1,column2,column3,column4) values VALUE from those two table
Some of the result from table contains null as well.Also i am using some mathematical function like sum on some column as well
Thanks in advance

Try out with following code:
INSERT INTO Temp_table (column1, column2, column3, column4)
SELECT column1, column2, ISNULL(column3,0), ISNULL(column4,0) FROM TABLE1 WHERE someCondition
UNION ALL
SELECT ISNULL(column1,0), ISNULL(column2,0), column3, column4 FROM TABLE1 WHERE someOtherCondition

You want to do something like:
INSERT INTO Temp_table (column1, column2, column3, column4)
SELECT column1, column2, NULL AS column3, NULL AS column4 FROM TABLE1 WHERE someCondition
UNION
SELECT NULL AS column1, NULL AS column2, column3, column4 FROM TABLE1 WHERE someOtherCondition

Related

Is is possible to use a CTE combined with a UDT to delete data?

I have a stored procedure that takes in a UDT. I would like to sanitize this data before inserting into the database to prevent any duplicate data from getting inserted. (Primary Key is still allowing duplicate data just the PK is different).
So I am using shorthand with a cte to get remove all of the possible duplicate rows and then use the clean data to insert into my table
The problem is I am getting an error that "#UDT_MyTable is READONLY and cannot be modified". Is there someway around this without going the temp table route?
Create Procedure InsertIntoMyTable
(
#UDT_MyTable dbo.MyUDT readonly
)
as
Begin
declare #TodaysDate datetime = CURRENT_TIMESTAMP
begin transaction
;with cte as
(
Select Row_Number() Over
(
PARTITION BY
Column1,
Column2,
Column3,
Column4,
Column5,
Column6
)
as sequence from
#UDT_MyTable
Errors Here
**Delete from cte Where sequence > 1**
insert into my table(
Column1,
Column2,
Column3,
Column4,
Column5,
Column6,
DateCreated
)
select
Column1,
Column2,
Column3,
Column4,
Column5,
Column6,
#TodaysDate
from cte

postgresql conditionnal insert values

I need a SQL which do following work.
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
This insert will only happen when another condition is TRUE, the
condition is:
SELECT 1 FROM table_2 WHERE column_time = 'time_stamp'
Table_1 and table_2 dont have any relation. the INSERT values to table_1 should only happen if the query on Table_2 return TRUE(1)
So i try to write my SQL as following
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
However, this SQL not work as Postgresql looks dont like the INSERT combine with WHERE condition.
BTW: the values (:1, :2:, :3) are bind array values. the final SQL looks like this:
INSERT INTO table_1 (column1, column2, column3) VALUES ('ca_1','cb_1', 'cc_1') ('ca_2','cb_2', 'cc_2') ... ('ca_n','cb_n', 'cc_n') WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'my_time_stamp') ON CONFLICT DO NOTHING
Really need help thanks.
You need to select those values.
This would work in Postgres:
INSERT INTO table_1 (column1, column2, column3)
SELECT :1, :2, :3
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
Alternatively:
INSERT INTO table_1 (column1, column2, column3)
SELECT x.*
FROM (VALUES (:1, :2, :3)) AS x(column1, column2, column3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING

MS SQL output inserted with insert from select

I'm trying to figure out how to combine INSERT FROM SELECT and returning id value of inserted record.
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
OUTPUT inserted.ID (SELECT TOP 1 590675, column2, column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817)
If you are trying to get the new ID after your INSERT statement you can use SCOPE_IDENTITY, IDENT_CURRENT or ##IDENTITY. For example:
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
SELECT TOP 1
590675,
column2,
column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817
ORDER BY ...
SELECT SCOPE_IDENTITY(); -- Last identity generated in current session and current scope
SELECT ##IDENTITY; -- Last identity generated in current session across all scopes
SELECT IDENT_CURRENT([someDB].[dbo].[OBJ]) -- Last identity generated for the given table in any session and any scope
Because you are inserting just one row, SCOPE_IDENTIY() would be the best approach.
Hope it helps.
Your syntax is a little wrong here.
You want:
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
OUTPUT inserted.ID
SELECT TOP 1
590675,
column2,
column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817
ORDER BY ...?; --You have a TOP 1, thus you really need an ORDER BY as well.

dividing values of 2 columns

How can I divide the values of 2 columns?
select column1, column2, (column1 / column2) as %ofSales ------
won't work for me.
Your query have not from clase
select column1, column2, (column1 / column2) as %ofSale
from my_table
and could be that the as column name with % should be wrapped in quotes
select column1, column2, (column1 / column2) as '%ofSale'
from my_table
You are probably getting a syntax error. Try with apostrophes like below
select column1, column2, (column1 / column2) as '%ofSales'
from yourTable

INSERT INTO subtract 2 values

Is it possible for an INSERT query to subtract 2 values you have entered to create a 3rd value that can then be inserted into a table - if that makes sense...
e.g.
INSERT INTO table1 (column1, column2, column3)
VALUES ('50', '25', column1 - column2)
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', column1 - column2) from table1 where conditions)
This is a sample query! hope it helps!
Convoluted:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
(select 50 as column1,
25 as column2
) t
Since you can't reference other columns from the same SELECT clause, you have to do it as a subquery. I've also switched to using int literals rather than strings, because I can't make subtraction make sense in my head otherwise.
You could also do it using a Table Value Constructor:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
( VALUES (50, 25)
) AS t (column1, column2);
As indicated in my comment though, if the relationship should always hold, I'd build table1 as:
CREATE TABLE table1 (
column1 int not null,
column2 int not null,
column3 as column1 - column2
--More columns
)
Because that way, the column3 value is always correct.
You can create function that subtracts values and use this function in insert. This is the right way to do such things:
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', your_function() ) from table1 where conditions)
/
Using the "INSERT INTO" would do this:
INSERT INTO Table1Name (column1, column2, column3,)
(select 'X', 'Y', X - Y as Z)
Here is a link to SQL Authority with more examples of INSERT INTO
Another method would to be add a trigger to the table, where on insert of data, the third column would be updated with the difference of the first two columns.