how to insert data from one table to another table(database) [duplicate] - sql-server-2005

This question already has answers here:
How to update data in one table from corresponding data in another table in SQL Server 2005
(8 answers)
Closed 9 years ago.
I have two databases (test1 and test2) on the same server, which have the same tables (Employee) with the same scheme. Employee holds around 1.500 rows.
Now I want to copy the value of column EmpDepID for each PK.
How can I achieve this?

UPDATE [test1].[dbo].[Employee]
SET [EmpDepID] = test2.[EmpDepID]
FROM [test2].[dbo].[Employee] test2
WHERE test2.[PK] = [test1].[dbo].[Employee].[PK]
as stated by #AdiInbar, the obvious intention of this question was something completely different.
original answer:
INSERT INTO [database1].[dbo].[table1]
(
/* TODO: define columns */
)
SELECT * /* or specify the columns */
FROM [database2].[dbo].[table2]

Related

How can I limit what data is entered in SQL Server? [duplicate]

This question already has answers here:
SQL add a new column and its values only can be in several fixed options
(2 answers)
Closed 7 months ago.
For example: I have a column named Software, where I want only 2 entries either Hotel or Restro. Besides these two;Hotel and Restro, the column should not allow other data. I am trying to implement this database in Hotel Managemnet System through ASP.NET. So, is it possible in anyway in SQL Server?
You may use a check constraint in the create table definition:
CREATE TABLE yourTable (
Id INT IDENTITY PRIMARY KEY,
some_col VARCHAR(6) NOT NULL CHECK (some_col IN ('Hotel', 'Restro'))
-- rest of definition here
);

Select values that are not included on a table [duplicate]

This question already has answers here:
How to select all records from one table that do not exist in another table?
(15 answers)
Closed 1 year ago.
I have one table "Cells" with Primary key "Cell_ID". There are 160 ID's/records. There is one other table, for example "Customers" where I use a field "CellID", which is a table list using row source the field "Cell_ID" from the table "Cells".
I would like to create a query that will return me all the Cell_ID values that are not used on the Customers.CellId field.
My first thought was to use something like the following:
SELECT Cells.Cell_ID
FROM Cells
WHERE Cells.Cell_ID NOT IN (
SELECT Customers.CellID
FROM Customers);
Your method is conceptually okay. But there is a problem if Customers.CellId is ever NULL. If that is the case, then the query will return no rows.
For this reason, I recommend never using NOT IN with a subquery. Instead, use NOT EXISTS:
SELECT c.Cell_ID
FROM Cells as c
WHERE NOT EXISTS (SELECT 1
FROM Customers as cu
WHERE c.Cell_ID = cu.CellID
);

PostgresSql error: more than one row returned by a subquery used as an expression [duplicate]

This question already has answers here:
Insert into ... values ( SELECT ... FROM ... )
(27 answers)
Closed 3 years ago.
i am running this query where in rel_branding there are more than 1 rows and i want to insert brandingid of every record in rel_branding_permission.
This is for PostgresSQL.
insert into rel_branding_permission (brandingid,permissionid)
values((select brandingid from rel_branding), 404);
I want to insert this query in my DB as for all branding ids
You should be using an INSERT INTO ... SELECT here, something like this:
INSERT INTO rel_branding_permission (brandingid)
SELECT brandingid
FROM rel_branding
WHERE <some condition>
You would probably want to include more than one column in the insert into the rel_branding_permission table, and you may modify the above to fit your need by adding the additional columns to both the INSERT and the SELECT.

How to make a column update automatically in another table when a record is inserted into the table [duplicate]

This question already has answers here:
mysql - Automatically update occurrences in another table
(2 answers)
Closed 4 years ago.
When a record is inserted into a table, QuantityInHand in the Items table should be updated automatically. The two tables are Transactions.OrderDetails and Items.ItemDetails.
I've tried something like this:
Create Trigger items.trgItemDetails
on items.ItemDetails
After update
As
Begin
Update items.ItemDetails
Set Items.itemDetails.QuantityInHand = ItemID
From Inserted
where inserted.ItemID = Items.ItemDetails
End
Please, how can I solve this problem?
Thanks.
you can achieve this using Triggers.
check the link below
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql

Getting Identity Column Value [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get the primary key of the last row inserted into the table
In SQL Server 2008, I have a stored proc that inserts in a table which includes identity column for ID. I need to return the ID of record to my application so that I can use it for related tables.
How can I get generated ID by SQL?
Use select Scope_Identity() to get the ID
A separate SQL statement
SELECT SCOPE_IDENTITY()
Or the OUTPUT clause
INSERT MyTable (...=
OUTPUT INSERTED.KeyCol
VALUES (...) --Or SELECT ... FROM Another table)
There are multiple options that are a bit different:
SCOPE_IDENTITY() - that's what I would use
IDENT_CURRENT( 'table_name' )
##IDENTITY