MS SQL output inserted with insert from select - sql

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.

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

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.

insert into with select + new information

I'm trying to write a sql query that can copy specific columns from a table and insert it in the same table + extra information for the other columns
Simply copying certain information would be something like this:
INSERT INTO table (column1, column2)
SELECT column1, column2
FROM table
WHERE columnx = 'some value'
But I need to also insert some new information in column3. How can I do that?
I have the information that will go in "column3", I don't have to get it from an other table or source.
This is for a repeat appointment where basically all the information is the same except for date, planner and appointment_id.
If you know what the values are . . .
INSERT INTO table (column1, column2, column3)
SELECT column1, column2, <value for column3>
FROM table
WHERE columnx = 'some value'

Populating temporary table with result of independent Sql Query

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

Grouping result according to one column in sql

I have a sql statement like
SELECT column1, column2,column3, column4 FROM table1 GROUP BY column3
or
SELECT * FROM table1 GROUP BY column3
I want a result which is grouped according to column3 and also has other columns. But it gives an error.What should I do? thanks..
If i understand your expected end result correctly,
try using ORDER instead of GROUP
if however that does not give you your desired output, i apologize.
SELECT column1, column2,column3, column4
FROM table1
ORDER BY column3
SELECT *
FROM table1
ORDER BY column3
What you are trying to do is an undefined behavior. When you specify GROUP BY on a column, there can be multiple values in other columns. There is no sane way for the database to figure out what values to show.
PARTITION is probably what you are looking for. Check this link: http://msdn.microsoft.com/en-us/library/ms189461.aspx
Grouping applies to aggregate functions such as SUM(). It doesn't make sense to group individual fields.
SELECT SUM(col1), MAX(col2), col3 from t1 group by col3
I haven't understood your question exactly and what for you need this query, but maybe you can use this one:
/*t-sql example*/
create table #T (id int identity(1,1), col1 varchar(5), col2 varchar(5), col3 varchar(6))
insert #T (col1,col2,col3) values ('aaa','a1e','group1')
insert #T (col1,col2,col3) values ('bbb','a2e','group1')
insert #T (col1,col2,col3) values ('ccc','a3e','group1')
insert #T (col1,col2,col3) values ('ddd','a4e','group2')
insert #T (col1,col2,col3) values ('eee','a5e','group2')
insert #T (col1,col2,col3) values ('fff','a6e','group3')
select id,col1,col2,#T.col3,subq.cnt from #T
join
(select col3, COUNT(*) as 'cnt' from #T group by col3) as subq
on
#T.col3 = subq.col3
I have used sub-query, but you can use temporary table or WITH. Also, I have used "count" for aggregation just for example.
Max