I have a table with Identity column set on ID
Ex:
Create table Customer
(
ID Int identity (1,1),
FirstName varchar(20),
PhNumber int,
)
I have a table on other database which populates this table
So I wrote something like
Create or alter view abcCustomer
As
Select
? as ID
Fname as FirstName,
Pnumber as phnumber
From dbo.Scustomer (this is my table in source)
Here we are creating ID column in target only but not sure how to call or what to call from source so that it doesn’t affect the AbcCustomer View and populate identity records in target table and also the FirstName and phNumber from the source table.
Could anyone help what should I write in place of ? In order to get my table populated as identity column, because unless and until we call the ID column from the source it’s gonna end up with having errors.
there are so many ways to get id and others data from source table!
Using Where condition (you can use stored procedure):
Declare #i as int = 1
Select Id, FName, PhNumber From dbo.SCustomer Where Id = #i
set #i = #i+1
Using Offset:
Declare #i as int = 1
Select Id, FName, PhNumber from Dbo.Sccustomer Order By Id Offset #i Rows Fetch
Next #i Rows Only
Set #i = #i+1
If you want to insert data from the source customer table to target customer table then you should write a query in this way -
Option 1 - If you want to use the identity same as in the source table then Turn on the identity so that you will be able to insert value in the identify column (refer below query)
SET IDENTITY_INSERT Customer ON
Insert Customer
(ID, FirstName, PhNumber)
Select
ID as ID
Fname as FirstName,
Pnumber as phnumber
From dbo.Scustomer
SET IDENTITY_INSERT Employee OFF
Option 2 - If you want to generate the new identity in the source table then use the below query.
Insert Customer
(FirstName, PhNumber)
Select
Fname as FirstName,
Pnumber as phnumber
From dbo.Scustomer
Related
enter image description hereI created a table and inserted 8 rows. Then I added a new column called Childname using alter. How can I add or insert different values into the new column at once without using update and Set for each row within the new column? I mean the Childname column showing the null values. I highlighted it with a red arrow.
As far as I understand your question;
You created table then add new column:
CREATE TABLE foo(id int, name varchar(255), city varchar(255));
INSERT INTO foo(id, name, city) VALUES (1, "John", "NYC");
INSERT INTO foo(id, name, city) VALUES (2, "Bill", "Boston");
....
# Alter table
ALTER TABLE foo ADD uid int;
After ALTER operation, surnames were null
If you want to add values to surname column of rows that already existed in the table, you need to use UPDATE like in below sql:
UPDATE foo SET uid = 1 WHERE id = 1
WHERE parameter can change by the case.
UPDATE:
After adding a new column to table which has rows, you could set that column with auto-increment property in SQL Server:
ALTER TABLE foo ADD uid INT IDENTITY(1, 1)
I need to insert records into a production table. The problem is that one of the fields needs to be the same value as the primary key.
In the example below, the Insert query is dropping '99' into [AlsoMyID]. But that's just a placeholder. It needs to be whatever value is going into [MyID].
How do I write the Insert query so that the system will add the same PK value to both [MyID] and [AlsoMyID]?
Drop table #mylittletable
Create table #Mylittletable (
[MyID] int IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[AlsoMyID] int,
[ActualData] varchar(1))
Select * from #Mylittletable
Insert into #Mylittletable values (99,'x')
Select * from #Mylittletable
If you're interested in the background, the developer is using AlsoMyID as a linking field so any number of records can be linked together using the original primary key value. That said, I have no control over the table structure.
Firstly, you cannot specify the value for identity column unless you use set identity_insert on. so according to your requirement, you need to insert the same value to AlsoMyID as MyID.
You can work it out as flowing:
insert into Mylittletable
select ##IDENTITY+1,'1'
With this trigger on the table you can insert anything on the alsoMyID-column and that will be overwritten with what get's set in the myID-column.
create trigger tr_Mylittletable ON Mylittletable
AFTER INSERT AS
BEGIN
declare #ID int = (select MyID from inserted)
update Mylittletable set AlsoMyID = #ID where MyID = #ID
END
NOTE: This only works when making inserts of one line at a time!
I have this data.
I want to duplicate data like a picture above with stored procedure.
First thing I do is copying two rows in the first table. How can I get 2 (two) 'iId' in the first table to create 2 (two) rows in the second table and put those 'iId' into 'iId_JTS-Rule_RulePricingGroup' like the picture above?
I think you can use OUTPUT clause with INSERT
CREATE TABLE #Table1(
ID int IDENTITY PRIMARY KEY,
Title varchar(10)
)
CREATE TABLE #Table2(
ID int,
Title varchar(10)
)
DECLARE #NewIDs TABLE(ID int)
INSERT #Table1(Title)
OUTPUT inserted.ID INTO #NewIDs(ID) -- save new IDs
VALUES ('A'),('B'),('C')
INSERT #Table2(ID,Title)
SELECT ID,Title
FROM #Table1
WHERE ID IN(SELECT ID FROM #NewIDs) -- use new IDs
DROP TABLE #Table1
DROP TABLE #Table2
I am trying to insert rows into a empty table by selecting rows from another table both tables containing same columns my query is
declare #name as varchar(50)
declare #address as varchar(50)
set #name=(select Name from Test.dbo.T_Sample)
set #address=(select Address from Test.dbo.T_Sample)
insert into Sam.dbo.T_Emp(Name,Address)values(#name,#address)
My tables are like this
T_Sample
Pk_Id Name Address
1 Sam Sam
2 A A
T_Emp
Pk_Id Name Address
Please help me Thank you
You can do it in one statement
insert into Sam.dbo.T_Emp (Name, Address)
select Name, Address from Test.dbo.T_Sample
I'm beginner in SQL Server and in my database I have two tables:
Table 1 with an id type bigint identity(1, 1)
Table 2 with the columns name and table1ID.
I want to insert (into table 1) and generate an id value. Then I will insert that id value into table1ID of table 2.
i change table to this and write this query:
insert into Interconnect_Traffic_Analysis_MAIN (Code_Op,Name_Op,Shomare_Tel,Duration,table1ID>>table1 id must save in this column)
select t7,t8,t9,t10 from Interconnect_Traffic_Analysis_Temp;
But how can I implement that?
Try this:
-- declare a variable to hold your newly created IDENTITY value
DECLARE #Identity BIGINT
-- insert your values into the first table
INSERT INTO dbo.Table1(list-of-columns)
VALUES(list-of-values);
-- get the newly generated IDENTITY value into your variable
SET #Identity = SCOPE_IDENTITY();
-- use that variable in your second INSERT
INSERT INTO dbo.Table2(table1Id)
VALUES(#Identity)
Update:
With your updated question with the INSERT statement, use this code to include the #Identity value:
INSERT INTO dbo.Interconnect_Traffic_Analysis_MAIN(Code_Op, Name_Op, Shomare_Tel, Duration, table1ID)
SELECT
t7, t8, t9, t10, #Identity
FROM
Interconnect_Traffic_Analysis_Temp;
You can try this:
insert into TableOne values ('abc');
insert into tableTwo values ((select SCOPE_IDENTITY()));
select SCOPE_IDENTITY() goes give you the last inserted id.
See this SQL fiddle