Showing posts with label table2. Show all posts
Showing posts with label table2. Show all posts

Tuesday, March 20, 2012

Add values to a table based on values in another table

When a record is inserted or updated records in Table1 I want a record to be inserted into table3 for each record ID that is in table2 if the table2.id does not already exist in table3. What is the best way to do this? Could this be done with a trigger? If it could how would I write such a trigger. I have never written one before and this sounds like a hand full for my first effort. Your help will be greatly appreciated.

Thanks

You could do it in a trigger. They're really not hard. It's just SQL you want to run whenever a certain action happens on your table. Pretty straightforward. Another option is to use stored procedures to do all of your inserts and updates. Then, just code what could go in the trigger directly into your stored procedure.

Pete

Sunday, March 11, 2012

add table from one DB into another DB on same sql server

Hi
Is there an easy way to add table from one database into another database on
the same sql server 2000?
e.g DB1 .....Table1, Table2...
add Table1, Table2... into DB2
Thank you.
ShaileshUSE DB2
CREATE TABLE dbo.Table1 (...)
CREATE TABLE dbo.Table2 (...)
INSERT INTO dbo.Table1 (...)
SELECT ...
FROM DB1.dbo.Table1
INSERT INTO dbo.Table2 (...)
SELECT ...
FROM DB1.dbo.Table2
David Portas
SQL Server MVP
--|||Use INSERT ... SELECT ...
Example:
use db1
go
insert into t (col1, col2, ...)
select col1, col2, ... from db2..t
go
AMB
"Shailesh Patel" wrote:

> Hi
> Is there an easy way to add table from one database into another database
on
> the same sql server 2000?
> e.g DB1 .....Table1, Table2...
> add Table1, Table2... into DB2
> Thank you.
> Shailesh
>
>|||If the table does not exists in the targer db, then use SELECT ... INTO ...
Example:
use db1
go
select col1, col2, ...
into db2..t
from t
go
No constraints, relations, indexes will be created.
AMB
"Alejandro Mesa" wrote:
> Use INSERT ... SELECT ...
> Example:
> use db1
> go
> insert into t (col1, col2, ...)
> select col1, col2, ... from db2..t
> go
>
> AMB
>
> "Shailesh Patel" wrote:
>|||note: SELECT ... INTO ... doesn't work unless the select into/bulkcopy optio
n
is set on the database. It should be emphasized that this option interferes
with some database recovery models, and thus should be avoided in a
production environment. Your original reply was the better solution.
"Alejandro Mesa" wrote:
> If the table does not exists in the targer db, then use SELECT ... INTO ..
.
> Example:
> use db1
> go
> select col1, col2, ...
> into db2..t
> from t
> go
> No constraints, relations, indexes will be created.
>
> AMB
>
> "Alejandro Mesa" wrote:
>|||'select into/bulkcopy' option is no longer needed in sql2k. Take a quick
look at the [INTO] section.
http://msdn.microsoft.com/library/e...sa-ses_9sfo.asp
-oj
"Brian Selzer" <BrianSelzer@.discussions.microsoft.com> wrote in message
news:C88C3B63-C3EE-4FFE-B9FC-3B24D1FCDB39@.microsoft.com...
> note: SELECT ... INTO ... doesn't work unless the select into/bulkcopy
> option
> is set on the database. It should be emphasized that this option
> interferes
> with some database recovery models, and thus should be avoided in a
> production environment. Your original reply was the better solution.
>
> "Alejandro Mesa" wrote:
>|||Thank you for clarifying that.
"oj" wrote:

> 'select into/bulkcopy' option is no longer needed in sql2k. Take a quick
> look at the [INTO] section.
> http://msdn.microsoft.com/library/e...sa-ses_9sfo.asp
>
> --
> -oj
>
> "Brian Selzer" <BrianSelzer@.discussions.microsoft.com> wrote in message
> news:C88C3B63-C3EE-4FFE-B9FC-3B24D1FCDB39@.microsoft.com...
>
>