SQL server 2005 :Updating one record from 2 identical records - sql-server

I have 2 records in a table in SQL Server 2005 db which has exactly same data.
I want to update one record.Is there anyway to do it?Unfortunately this table does not have an identity column and i cant use a direct update query because both will be updated since data is same.Is there anyway using rowid or something in SQL server 2005 ?

I don't much like the TOP operator, but:
UPDATE top (1) MyTable
set Data = '123'
where Data = 'def'
Really, you want to have primary keys on your tables to avoid just this kind of situation, even if they are just identity surrogate values.

I would add an identity column to the table and then update on that identity column or update based on whatever the primary key of the table is that makes the row unique.

Related

Identity column in view of SQL Server

I have a table in which has a Identity Column.
When I tried to create a view (with only this table), everything is still working fine, the ID property(which is used to mark auto increment number column) of Identity Column is still set to True
When there is only 1 table in view
Here is the diagram
But when I tried to create a view by join the mentioned above table with another table, the Identity Column is automatically set to false.
When I try to join 2 tables in view
Here is the diagram
This problem occurs maybe only in SQL Server 2016 (I tried with SQL Server 2008, it's still working fine)
Did anyone encounter similar problems? How to keep true value of Identity Column in View? Thanks

Transfer data from one database to another database with different schema

i have problem to Transfer data from one sqlserver 2008 r2 to another sql server 2012 databases with different schema, here is some different scenario,
database 1
database 1 with tables Firm and Client, these both have FirmId and ClientId primary key as int datatype,
FirmId is int datatype as reference key used in Client table.
database 2
database 2 with same tables Firm and Client, these both have FirmId and ClientId but primary key as uniqueidentifier,
FirmId is uniqueidentifier datatype as reference key used in Client table.
problem
the problem is not to copy data from 1 database table to 2 database table, but the problem is to maintain the reference key's Firm table into Client table. because there is datatype change.
i am using sql server 2008 r2 and sql server 2012
please help me to resolve / find the solution, i really appreciate your valuable time and effort. thanks
I'll take a stab at it even if I am far from an expert on SQLServer - here is a general procedure (you will have to repeat it for all tables where you have to replace INT with UID, of course...).
I will use Table A to refer to the parent (Firm, if I understand your example clearly) and Table B to refer to the child (Client, I believe).
Delete the relations pointing to Table A
Remove the identity from the id column of Table A
Create a new column with Uniqueidentifier on Table A
Generate values for the Uniqueidentifier column
Add the new Uniqueidentifier column in all the child tables (Table B)
Use the OLD id column to map your child record & update the new Uniqueidentifier value from your parent table.
Drop all the id columns
Recreate the relations
Having said that, I just want to add a warning to you: converting to UID is, according to some, a very bad idea. But if you really need to do that, you can script (and test) the above mentioned procedure.

Sequences in Oracle to Identity columns in SQL Server

I need to 'migrate' a table in Oracle, this table has a primary key column with a sequence and a trigger to autoincrement this column mentioned, the process is detailed here.
The question is, I want to migrate this table to SQL Server put I want to take advantage of the identity feature. How can I tweak the table in SQL Server? Taking in count the fact I need to migrate the data and I don't want problems with the autoincrement column; will I lost the previous id assignation in Oracle?
No, you create the table with an identity column, like
MyID int identity([your max ID],1)
then when you insert the oracle data, prior to the insert run this command
SET IDENTITY_INSERT MYTABLE ON
/*insert your records*/
SET IDENTITY_INSERT MYTABLE OFF

import sybase data to sql server when any change make in table

I have a Sybase database which i want to migrate to SQL SERVER 2008R2. I have done this, but i got a new requirement. When any data is modified or new insert in table in Sybase that data only migrate from Sybase to Sql Server. The one table data is around 11,000135 so every time this is not possible to migrate all the data from Sybase to Sql Server. Is any possible way to do this?
I don't see any straight forward solution here. I would use following steps to deal with that issue:
Create table with unique key of source_table:
Create table mod_date
(
key int unique,
modified_date datetime
)
Create insert/update trigger for source_table that will be inserting/updating modified_date table.
When selecting data from source_table join mod_date and filter out only dates grater than last update.
Opposite to create new table yuo could also add modified_date to your source_table and use it during select. GL!

Some tables id column do not auto increment despite being identity

I am troubleshooting a db (not my forte) that has some tables that are auto incrementing on the id column and some tables are not?
Now all the tables are set as identity and marked to disallow null. I am using SSMS what else can I check or do to get these tables back to auto incrementing?
TIA
Interestingly to me...probably old news to you guys. The issue had to do with existing data. So for example a table that had 100 rows in did NOT have the identity column setup. So I would go in and make it an identity with a seed of 1 incrementing 1. Well the seed was somehow having trouble because there was already 100 rows in there. So for all my tables I had to do a row count and seed the identity from the next row. Now it is working as expected.
Having an IDENTITY column is pretty straightforward for a table, and if you are not seeing the auto incrementing behavior of a column on inserts, then I'd first verify that your column is indeed an IDENTITY column:
use <Your Database Name>;
go
select
name,
is_identity
from sys.columns
where object_id = object_id('<Your Table Name>')
order by column_id;
Substitute <Your Database Nam> and <Your Table Name> for their appropriate values.
The other possibility is that data that "appears" to be non-incrementing could have been pushed out to that with a session that set identity insert and pushed out explicit values.
ALTER TABLE YourTable MODIFY COLUMN YourTable_id int(4) auto_increment

Resources