I want to produce a map that contains the original room ID and its new room ID equivalent for every room created.
Try using the "OUTPUT" clause with your insert statement. You can then map all of the inserted values and the keys that went with it to a different "mapping" table. Edit: I should add, this is all done in a SINGLE transaction, which makes the performance GREAT!
Insert Into TableA
(
Field1,
Field2,
Field3
)
Output Inserted.Field1, Field2, Field3, MappingID
Into MappingTable
Select Field1, Field2, Field3
From Rooms
Here's a link to the msdn site for the OUTPUT clause: http://msdn.microsoft.com/en-us/library/ms177564.aspx
Though I would suggest you to alter the table design and have a separate column each for New and Original site Id, but in present design I think you are looking for following:
SELECT
Name, #origSiteID, ControlsSiteNum, ControlsRoomNum, IsActive
FROM Rooms
WHERE SiteID = #newSiteID
Related
Using MS Access, via VBA, I have been trying to update an main Access database table from an identically structured external Access database table. Same columns, same fields. The external databases are used to update a central main database.
What I have been trying to develop is a line of SQL that only appends the entries that are completely unique. Because there is no foreign keys or unique identifiers for the incoming data that can be referenced, I am required to check each field to make sure that there are no exact duplicates. So, if the table had 2 fields, if entries failed the logic test
intTable.field1 = extTable.field1 And intTable.field2 = extTable.field2
then those are the entries that would be appended.
The code I came up with is below, and when it runs it tries to append (in a Gary Oldman voice) EVERYTHING. I can't find out what could be wrong with it, as it's almost like it ignores the WHERE or WHERE NOT EXIST. I have tried dozens of small edits and alternate versions. Either is appends all or none.
INSERT INTO Table1
SELECT field1, field2, field3, field4, field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB1
WHERE ('Table1.[field1]'='sourceDB1.[field1]' And
'Table1.[field2]'='sourceDB1.[field2]' And
'Table1.[field3]'='sourceDB1.[field3]' And
'Table1.[field4]'='sourceDB1.[field4]' And
'Table1.[field5]'='sourceDB1.[field5]'));
FINAL CODE (after implementing ArcherBird's solution):
INSERT INTO Table1
SELECT field1, field2, field3, field4, field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB1
WHERE ((sourceDB.[field1] = sourceDB1.[field1] Or (sourceDB.[field1] IS NULL And sourceDB1.[field1] IS NULL)) And
(sourceDB.[field2] = sourceDB1.[field2] Or (sourceDB.[field2] IS NULL And sourceDB1.[field2] IS NULL)) And
(sourceDB.[field3] = sourceDB1.[field3] Or (sourceDB.[field3] IS NULL And sourceDB1.[field3] IS NULL)) And
(sourceDB.[field4] = sourceDB1.[field4] Or (sourceDB.[field4] IS NULL And sourceDB1.[field4] IS NULL)) And
(sourceDB.[field5] = sourceDB1.[field5] Or (sourceDB.[field5] IS NULL And sourceDB1.[field5] IS NULL)) ));
1) Remove the string quotes from your field comparison logic.
2) You have aliased Table1 in your query as sourceDB, so use that reference.
3) The table where you check for non-existence ought to be the "local" version of Table1 (the one you are inserting into).
INSERT INTO Table1
SELECT field1,
field2,
field3,
field4,
field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [Table1] sourceDB1
WHERE (sourceDB.[field1] = sourceDB1.[field1] And
sourceDB.[field2] = sourceDB1.[field2] And
sourceDB.[field3] = sourceDB1.[field3] And
sourceDB.[field4] = sourceDB1.[field4] And
sourceDB.[field5] = sourceDB1.[field5]));
EDIT:
To deal with the case where these fields might have null values, you cannot do a simple = comparison since null does not equal null. The best way I can think of to get around this in MS Access would be to replace null with some other value that is not likely to be used. In doing so, you also need to make sure you chose replacement values that are consistent with the data type. I will make an assumption that all your fields are text.
INSERT INTO Table1
SELECT field1,
field2,
field3,
field4,
field5
FROM [;DATABASE=C:\extDB.accdb].[Table1] sourceDB
WHERE NOT EXISTS (SELECT *
FROM [Table1] sourceDB1
WHERE (nz(sourceDB.[field1],"!##") = nz(sourceDB1.[field1],"!##") And
nz(sourceDB.[field2],"!##") = nz(sourceDB1.[field2],"!##") And
nz(sourceDB.[field3],"!##") = nz(sourceDB1.[field3],"!##") And
nz(sourceDB.[field4],"!##") = nz(sourceDB1.[field4],"!##") And
nz(sourceDB.[field5],"!##") = nz(sourceDB1.[field5],"!##")));
I'm trying to create a temporary table with a select into statement in a stored procedure as follows:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
)
Any command I type after this closing bracket fails the syntax check (such as another SELECT statement).
I've tried putting BEGIN and END before and after the whole statement and then starting my next command.
I've tried adding
AS tablename
after the closing bracket and then the next statement but it doesn't like that either
I've tried removing the # but same problem.
I actually need to run a WHILE loop after this and INSERT more records into the same table.
What am i doing wrong?
thanks
Out of curiosity, why are you using a subquery?
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
INTO #GENEAOLOGY
FROM table
WHERE condition;
The structure of your query is ok. You should include a semi-colon at the end, if this is in a programming block. I assume table is a real name and that the other parts of the query are syntactically correct.
What does the error mention? If you run this in SSMS more than once, the second time the temp table already exists and could be an error.
You said an alias was not enough, but this is a right sentence:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
) as TableNameAlias
thanks guys for the help
in the end I changed to an INSERT INTO rather than a SELECT INTO because
a) i needed to do 2 different select queries to get the data i need (1 for the first record and a 2nd for all subsequent records) and the second query would have had to be an INSERT anyway
b) and i wasn't sure how long sql server would keep the temp table open if i was calling the procedure from a crystal report
so i created a fixed table, use SELECT INTO and just delete all records as first line of my sp to clear out unwanted data
ta!
Below is a table im working with. I would like to take the field from [Field1] column and move directly across to [Field2] column.
Results would be like this
Field2
Browser
com.android.browser
com.android.browser.BrowserActivity
Email
com.android.email
com.android.email.activity.Welcome
Phone
com.android.contacts
com.android.contacts.activities.PCUDialtactsActivity
Gallery
com.android.gallery3d
com.android.gallery3d.app.Gallery
Tắt mà n hình
com.katecca.screenofflockdonate
com.katecca.screenofflockdonate.MainHelper
Messaging
com.android.mms
com.android.mms.ui.ConversationList
Simple mode
com.pantech.app.skysettings.simplemode
com.pantech.app.skysettings.simplemode.simplemodesettingshrotcutActivity
This might do what you need:
SELECT iif(isnull(Field2), Field1, Field2) as [yourColumnTitle]
FROM yourTableName
Aside from this you'll need to add a column to the table datatype=YesNo and set it to true on the rows you want to affect, then you can
SELECT iif(NewColumn <> 0, Field1, Field2) as [yourColumnTitle]
FROM yourTableName
Icurrently have in my database (SQL server 2005 Express) 2 tables, tblDepartment and tblDepartmentcola. The difference between the two is that "cola" has an additional boolean field. For reasons beyond the scope of this question, i need to insert any changes made to tblDepartment in tblDepartmentcola. For that i need to use a trigger.
After reading some stuff, i got the impression that a temporary "updated" table does not exist? is this right?. If so, how could i select the "updated" row?, i have made triggers in the past making selects from "updated" and "Deleted" tables, but this one does not work. any idea why?
CREATE TRIGGER items_ ON [tblDepartment] FOR Update AS
INSERT INTO [tblDepartmentcola]
SELECT ...
go
there's no updated table in Sql server, only inserted and deleted
CREATE TRIGGER items_ ON [tblDepartment] after Update AS
begin
INSERT INTO [tblDepartmentcola]
SELECT * from inserted
end
If you want to see the records that were updated to insert into a different table you can query the INSERTED table within a trigger when the primary key exists within the DELETED table.
INSERT INTO TrackUpdatesTable ( PrimaryFieldId, Field1, Field2, Field3 )
SELECT I.PrimaryFieldId, I.Field1, I.Field2, I.Field3
FROM INSERTED I
JOIN DELETED D ON D.PrimaryFieldId = I.PrimaryFieldId
or
INSERT INTO TrackUpdatesTable ( PrimaryFieldId, Field1, Field2, Field3 )
SELECT I.PrimaryFieldId, I.Field1, I.Field2, I.Field3
FROM INSERTED I
WHERE EXISTS ( SELECT * FROM DELETED WHERE PrimaryFieldId = I.PrimaryFieldId )
Hope this helps.
I have two data base named:
MEX
USA
both containg a table named: BOM,
I would like to compare these two tables to know what records (Column Named IDBOM) are not on USA data base.
Can you publish the Query please?
I assume this is Oracle.
select field1, field2, field3
from tableA
MINUS
select field1, field2, field3
from tableB
will show you rows in tableA that are not in tableB based on these fields.
You can go across a dblink if needed (...from tableB#someLink)