Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

Monday, March 26, 2012

generation of sql for an alter column etc

Hi.

I have a database I need to supply something (I'm assuming a t-sql script..
maybe something else is better) to update customer tables with.
The operations include mostly changing varchar lengths, though a couple of
columns were renamed.

I'd like to maybe figure out how to get Enterprise Manager or Query Analyzer
to generate the scripts.

I can't just send alter table scripts because I'm involving all sorts of
constraints that have to be disabled/or dropped, the alter made, then have
them enabled/ or re-created.

Basically I'm hoping to get the tools to do the rather large amount of work
for me. I'm targetting sql server 2000.

Can someone make a knowledgeable suggestion?

Regards
Jeff KishJeff Kish wrote:
> Hi.
> I have a database I need to supply something (I'm assuming a t-sql script..
> maybe something else is better) to update customer tables with.
> The operations include mostly changing varchar lengths, though a couple of
> columns were renamed.

Not a good idea IMHO although you can use sp_rename.

> I'd like to maybe figure out how to get Enterprise Manager or Query Analyzer
> to generate the scripts.
> I can't just send alter table scripts because I'm involving all sorts of
> constraints that have to be disabled/or dropped, the alter made, then have
> them enabled/ or re-created.

Then generate the SQL for the target state and insert drops yourself.

> Basically I'm hoping to get the tools to do the rather large amount of work
> for me. I'm targetting sql server 2000.
> Can someone make a knowledgeable suggestion?

I don't think you will be able to get this out of EM - at least not
directly. It would basically mean to trace your operations and generate
SQL from that. I don't think EM will do that for such a complex set of
operations. You'll have to do some manual work.

Kind regards

robert|||Jeff Kish (jeff.kish@.mro.com) writes:
> I have a database I need to supply something (I'm assuming a t-sql
> script.. maybe something else is better) to update customer tables with.
> The operations include mostly changing varchar lengths, though a couple
> of columns were renamed.
> I'd like to maybe figure out how to get Enterprise Manager or Query
> Analyzer to generate the scripts.
> I can't just send alter table scripts because I'm involving all sorts of
> constraints that have to be disabled/or dropped, the alter made, then have
> them enabled/ or re-created.
> Basically I'm hoping to get the tools to do the rather large amount of
> work for me. I'm targetting sql server 2000.

Composing change scripts for production environments is not a task to
take lightly. Particularly not if you have to apply them while the system
is operating. (If the system closes for business, you may be able to repair
a disaster by restorin a backup.)

It requires good understanding of what can go wrong, and how to prevent
that. For instance, if you need to drop constraints to alter a column,
you should probably wrap that in a transaction, so you don't end up with
losing the constraint.

At the same time, ALTER operations that require changes to the physical
data pages, can take a huge toll on the transaction log, causing it to
grow rapidly. (Changing varchar lengths should be metadata so that should
be safe.)

You can use Enterprise Manager to have it to generate change scripts.
However, there are several flaw in those scripts, and you need to review
them carefully, and also make several changes to them. For instance, the
transaction scope in those scripts are wacko.

What may prove to be a show-stopper is that EM works with SQL 6.5 as its
target DB (same in Mgmt Studio in SQL 2005, by the way). 6.5 did not
have ALTER TABLE ALTER COLUMN, so I would guess that it implements the
update as create new table and copy data over. Which sometimes is the right
thing, but not when ALTER TABLE ALTER COLUMN is only a metadata change.

There are other tools on the market. Red Gate's SQL Compare get a lot
of positive acclaim, but I have not used it myself.

One potential problem is that you don't know the name of the constraints,
because they were added without a name, so all there is a system-generated
name. In this case, you need to retrieve the name, and then run DROP
CONSTRAINT dynamically. I would suggest that you restore the constraints
with a given name.

Speaking of constraints, make sure that you re-add them WITH CHECK. The
script from EM uses WITH NOCHEK, which means that they are not checked. This
is a lot faster, but it also means that the optimizer will neglect them,
which can have expensive consequences.

Finally, before you run in production, test on a copy of production!

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||On Fri, 24 Mar 2006 13:45:19 +0000 (UTC), Erland Sommarskog
<esquel@.sommarskog.se> wrote:

>Jeff Kish (jeff.kish@.mro.com) writes:
>> I have a database I need to supply something (I'm assuming a t-sql
<snip>
>Composing change scripts for production environments is not a task to
>take lightly. Particularly not if you have to apply them while the system
>is operating. (If the system closes for business, you may be able to repair
>a disaster by restorin a backup.)
>It requires good understanding of what can go wrong, and how to prevent
>that. For instance, if you need to drop constraints to alter a column,
>you should probably wrap that in a transaction, so you don't end up with
>losing the constraint.
ahh.. I had no idea that transactions could wrap/rollback ddl. I don't think
that is the case in Oracle.
>At the same time, ALTER operations that require changes to the physical
>data pages, can take a huge toll on the transaction log, causing it to
>grow rapidly. (Changing varchar lengths should be metadata so that should
>be safe.)
>You can use Enterprise Manager to have it to generate change scripts.
>However, there are several flaw in those scripts, and you need to review
>them carefully, and also make several changes to them. For instance, the
>transaction scope in those scripts are wacko.
Can you tell me how? I'm having some problem seeing how to get the equivalent
alter table etc scripts out of EM. I looked, honest. I'll even look some more.

>What may prove to be a show-stopper is that EM works with SQL 6.5 as its
>target DB (same in Mgmt Studio in SQL 2005, by the way). 6.5 did not
>have ALTER TABLE ALTER COLUMN, so I would guess that it implements the
>update as create new table and copy data over. Which sometimes is the right
>thing, but not when ALTER TABLE ALTER COLUMN is only a metadata change.
target is only sql server 2000 right now.
>There are other tools on the market. Red Gate's SQL Compare get a lot
>of positive acclaim, but I have not used it myself.
>One potential problem is that you don't know the name of the constraints,
>because they were added without a name, so all there is a system-generated
>name. In this case, you need to retrieve the name, and then run DROP
>CONSTRAINT dynamically. I would suggest that you restore the constraints
>with a given name.
mmm not sure I understand.. they are originally added specifically. can I just
disable them or do I need to drop them?

>Speaking of constraints, make sure that you re-add them WITH CHECK. The
>script from EM uses WITH NOCHEK, which means that they are not checked. This
>is a lot faster, but it also means that the optimizer will neglect them,
>which can have expensive consequences.
>Finally, before you run in production, test on a copy of production!
of course! the scars I have should remind me of that. :> )

thanks so much.
Jeff Kish|||Jeff Kish (jeff.kish@.mro.com) writes:
>>You can use Enterprise Manager to have it to generate change scripts.
>>However, there are several flaw in those scripts, and you need to review
>>them carefully, and also make several changes to them. For instance, the
>>transaction scope in those scripts are wacko.
>>
> Can you tell me how? I'm having some problem seeing how to get the
> equivalent alter table etc scripts out of EM. I looked, honest. I'll
> even look some more.

Right-click table and select Modify Table. As I said, it is not likely
it will generate ALTER TABLE commands, those you will have to write
yourself. But at least you will get some code to recreate constraints.
Just be sure to change WITH NOCHECK to WITH CHECK.

>>One potential problem is that you don't know the name of the constraints,
>>because they were added without a name, so all there is a system-generated
>>name. In this case, you need to retrieve the name, and then run DROP
>>CONSTRAINT dynamically. I would suggest that you restore the constraints
>>with a given name.
>>
> mmm not sure I understand.. they are originally added specifically. can
> I just disable them or do I need to drop them?

The error message from ALTER TABLE makes it clear that you have to
drop the constraint. Keep in mind that a FK column must match the
PK column it refers to, so if you change a PK/FK pair, you need to
drop constraints for both columns before changing.

Yes, it follows that from what I say that you should mainly generate
the script manually. This may seem like a crazy idea, but since it's
so important that you understand what the script does, it can be
dangerous to leave that in the hands of a tool.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Friday, March 9, 2012

generate update statements for existing data

Does anyone know how to write scripts for generating update statements for
existing data?
I found a stored procedure online that generates INSERT statements for a
given table, I was wondering if anyone has worked on a UPDATE generatorMike
What do you mean by "generating update"?
Do you want to build a script that does updateting?
declare @.sql varchar(400)
set @.sql='update '+ @.tablename+' set col'..........
"Mike" <Mike@.discussions.microsoft.com> wrote in message
news:9E10B2B9-80CC-4E50-9E34-5865A0C3BA1D@.microsoft.com...
> Does anyone know how to write scripts for generating update statements for
> existing data?
> I found a stored procedure online that generates INSERT statements for a
> given table, I was wondering if anyone has worked on a UPDATE generator|||I have a tables with about 1000 records and if I make changes I would like t
o
generate script (UPDATE scripts) for the existing data that I could run on
another server or ship it to a client with the latest changes instead of a
restore.
there is a good insert script available at
http://vyaskn.tripod.com/code.htm#inserts , I am looking for something
similar for UPDATE
"Uri Dimant" wrote:

> Mike
> What do you mean by "generating update"?
> Do you want to build a script that does updateting?
>
> declare @.sql varchar(400)
> set @.sql='update '+ @.tablename+' set col'..........
>
> "Mike" <Mike@.discussions.microsoft.com> wrote in message
> news:9E10B2B9-80CC-4E50-9E34-5865A0C3BA1D@.microsoft.com...
>
>|||Mike
Do you consider using Replication/Triggers ? It will be much better than
generating SQL script.
If you use SQL Server 2005 you can create a SNAPSHOP database ( ah i forgot
, it should be on the same server),sorry.
"Mike" <Mike@.discussions.microsoft.com> wrote in message
news:00EF43C1-EBF7-4422-99D0-A1979F3500A0@.microsoft.com...
>I have a tables with about 1000 records and if I make changes I would like
>to
> generate script (UPDATE scripts) for the existing data that I could run on
> another server or ship it to a client with the latest changes instead of a
> restore.
> there is a good insert script available at
> http://vyaskn.tripod.com/code.htm#inserts , I am looking for something
> similar for UPDATE
> "Uri Dimant" wrote:
>|||Mike wrote:
> I have a tables with about 1000 records and if I make changes I would like
to
> generate script (UPDATE scripts) for the existing data that I could run on
> another server or ship it to a client with the latest changes instead of a
> restore.
> there is a good insert script available at
> http://vyaskn.tripod.com/code.htm#inserts , I am looking for something
> similar for UPDATE
>
http://www.red-gate.com/products/SQ...mpare/index.htm
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||Try www.sqlscripter.com to generate data scripts. All types are supported
(Insert, Update, Delete).
It's free.
Thomas
"Mike" wrote:

> Does anyone know how to write scripts for generating update statements for
> existing data?
> I found a stored procedure online that generates INSERT statements for a
> given table, I was wondering if anyone has worked on a UPDATE generator

Wednesday, March 7, 2012

Generate Sequential Numbers - T-SQL

Hi
i have a table with a field called response id. i would like to update this
field with sequential numbers starting from 1000.
can someone please advise me how i go about this.
any help would be appreciated.
thanks heaps in advance
Kind Regards
RidhimaTested on NORTHWIND:
alter table ORDERS
add RESPONSEID int
GO
update ORDERS
set RESPONSEID = RowNumber + 999
from ORDERS inner join
(select ROW_NUMBER() OVER (order by OrderID) as RowNumber, ORDERID from
ORDERS) as TMP on
TMP.ORDERID = ORDERS.ORDERID
--
Alain Quesnel
alainsansspam@.logiquel.com
www.logiquel.com
"Ridhima" <Ridhima@.discussions.microsoft.com> wrote in message
news:65055160-3C5A-4278-B453-C4DA6344731D@.microsoft.com...
> Hi
> i have a table with a field called response id. i would like to update
> this
> field with sequential numbers starting from 1000.
> can someone please advise me how i go about this.
> any help would be appreciated.
> thanks heaps in advance
> Kind Regards
> Ridhima|||On Nov 15, 9:14 pm, Ridhima <Ridh...@.discussions.microsoft.com> wrote:
> Hi
> i have a table with a field called response id. i would like to update this
> field with sequential numbers starting from 1000.
> can someone please advise me how i go about this.
> any help would be appreciated.
> thanks heaps in advance
> Kind Regards
> Ridhima
Another way to control this moving forward is to select Design for the
table in SSMS and select the ResponseID field. In the Column
Properties section, select Identity Specification >> (Is Identity) and
set it to Yes. Set Identity Increment to 1 and then set Identity Seed
to 1000 (which is the starting integer value of the ResponseID field).
Hope this helps.
Regards,
Enrique Martinez
Sr. Software Consultant|||That was my first thought too. But when I tested it, it created "random"
values in the ResponseID field.
--
Alain Quesnel
alainsansspam@.logiquel.com
www.logiquel.com
"EMartinez" <emartinez.pr1@.gmail.com> wrote in message
news:29bb8aeb-5839-42b5-8e27-57978af3f160@.v4g2000hsf.googlegroups.com...
> On Nov 15, 9:14 pm, Ridhima <Ridh...@.discussions.microsoft.com> wrote:
>> Hi
>> i have a table with a field called response id. i would like to update
>> this
>> field with sequential numbers starting from 1000.
>> can someone please advise me how i go about this.
>> any help would be appreciated.
>> thanks heaps in advance
>> Kind Regards
>> Ridhima
>
> Another way to control this moving forward is to select Design for the
> table in SSMS and select the ResponseID field. In the Column
> Properties section, select Identity Specification >> (Is Identity) and
> set it to Yes. Set Identity Increment to 1 and then set Identity Seed
> to 1000 (which is the starting integer value of the ResponseID field).
> Hope this helps.
> Regards,
> Enrique Martinez
> Sr. Software Consultant|||declare @.secuence int
set @.secuence = 999
update table
set @.secuence = response_id = @.secuence + 1
PS.
It does work ! Have faith !
"Ridhima" <Ridhima@.discussions.microsoft.com> escribió en el mensaje
news:65055160-3C5A-4278-B453-C4DA6344731D@.microsoft.com...
> Hi
> i have a table with a field called response id. i would like to update
> this
> field with sequential numbers starting from 1000.
> can someone please advise me how i go about this.
> any help would be appreciated.
> thanks heaps in advance
> Kind Regards
> Ridhima

Sunday, February 26, 2012

generate real date time

hi, i need to create a function to do some like this...
update myTable set DateModify = getdate()
... but... i need to get a minimum difference bettwen records...
milisecons... or...
how can i get an array or cursor (SQL server side) with 500 records
with different datetime programatically?Hi,
I assume that you are asking about writing a procedure for this and not
a function. A function cannot update tables nor can it use getdate().
To do this in a procedure you can use the WAITFOR DELAY statement in
SQL.
Regards,
Kart
celerno wrote:
> hi, i need to create a function to do some like this...
> update myTable set DateModify = getdate()
> ... but... i need to get a minimum difference bettwen records...
> milisecons... or...
> how can i get an array or cursor (SQL server side) with 500 records
> with different datetime programatically?|||Hi Kart.. thanks for your response...
I try to use waitfor, but i can't find the way to do it.
sorry, i don't have good programming skills in sql server...
here is my problem:
i retrieve a bunch of data using the DateTime field to get the lastest
data using "select top N... ... where dateTimeNew>myDate"
after download the N rows, myDate gets the biggest dateTime value from
the N rows.
and when we have N+1 data with the same DateTime my new select Top is
equal to the previus sentence and i got the same data bunch.
in order to evade this problem i want to create a routine on store
procedure or any way on the server side to set different datetimes to
all data...
excuse my poor english... thanks in advance...!
On 10 nov, 13:08, "Kart" <skarthike...@.bigfoot.com> wrote:[vbcol=seagreen]
> Hi,
> I assume that you are asking about writing a procedure for this and not
> a function. A function cannot update tables nor can it use getdate().
> To do this in a procedure you can use the WAITFOR DELAY statement in
> SQL.
> Regards,
> Kart
> celerno wrote:
>
>
>|||yeah, this query it all that i need... it was so simple.!
reference to:
http://www.sqlteam.com/item.asp?ItemID=765
declare @.intCounter datetime
set @.intCounter = getdate()
update articulo
SET @.intCounter = fechahora = dateadd(s, 1, @.intCounter)
select fechahora from articulo
On 11 nov, 11:06, "celerno" <cele...@.gmail.com> wrote:[vbcol=seagreen]
> Hi Kart.. thanks for your response...
> I try to use waitfor, but i can't find the way to do it.
> sorry, i don't have good programming skills in sql server...
> here is my problem:
> i retrieve a bunch of data using the DateTime field to get the lastest
> data using "select top N... ... where dateTimeNew>myDate"
> after download the N rows, myDate gets the biggest dateTime value from
> the N rows.
> and when we have N+1 data with the same DateTime my new select Top is
> equal to the previus sentence and i got the same data bunch.
> in order to evade this problem i want to create a routine on store
> procedure or any way on the server side to set different datetimes to
> all data...
> excuse my poor english... thanks in advance...!
> On 10 nov, 13:08, "Kart" <skarthike...@.bigfoot.com> wrote:
>
>
>
>
>
>
>
>

generate real date time

hi, i need to create a function to do some like this...
update myTable set DateModify = getdate()
... but... i need to get a minimum difference bettwen records...
milisecons... or...
how can i get an array or cursor (SQL server side) with 500 records
with different datetime programatically?Hi,
I assume that you are asking about writing a procedure for this and not
a function. A function cannot update tables nor can it use getdate().
To do this in a procedure you can use the WAITFOR DELAY statement in
SQL.
Regards,
Kart
celerno wrote:
> hi, i need to create a function to do some like this...
> update myTable set DateModify = getdate()
> ... but... i need to get a minimum difference bettwen records...
> milisecons... or...
> how can i get an array or cursor (SQL server side) with 500 records
> with different datetime programatically?|||Hi Kart.. thanks for your response...
I try to use waitfor, but i can't find the way to do it.
sorry, i don't have good programming skills in sql server...
here is my problem:
i retrieve a bunch of data using the DateTime field to get the lastest
data using "select top N... ... where dateTimeNew>myDate"
after download the N rows, myDate gets the biggest dateTime value from
the N rows.
and when we have N+1 data with the same DateTime my new select Top is
equal to the previus sentence and i got the same data bunch.
in order to evade this problem i want to create a routine on store
procedure or any way on the server side to set different datetimes to
all data...
excuse my poor english... thanks in advance...!
On 10 nov, 13:08, "Kart" <skarthike...@.bigfoot.com> wrote:
> Hi,
> I assume that you are asking about writing a procedure for this and not
> a function. A function cannot update tables nor can it use getdate().
> To do this in a procedure you can use the WAITFOR DELAY statement in
> SQL.
> Regards,
> Kart
> celerno wrote:
> > hi, i need to create a function to do some like this...
> > update myTable set DateModify = getdate()
> > ... but... i need to get a minimum difference bettwen records...
> > milisecons... or...
> > how can i get an array or cursor (SQL server side) with 500 records
> > with different datetime programatically?|||yeah, this query it all that i need... it was so simple.!
reference to:
http://www.sqlteam.com/item.asp?ItemID=765
declare @.intCounter datetime
set @.intCounter = getdate()
update articulo
SET @.intCounter = fechahora = dateadd(s, 1, @.intCounter)
select fechahora from articulo
On 11 nov, 11:06, "celerno" <cele...@.gmail.com> wrote:
> Hi Kart.. thanks for your response...
> I try to use waitfor, but i can't find the way to do it.
> sorry, i don't have good programming skills in sql server...
> here is my problem:
> i retrieve a bunch of data using the DateTime field to get the lastest
> data using "select top N... ... where dateTimeNew>myDate"
> after download the N rows, myDate gets the biggest dateTime value from
> the N rows.
> and when we have N+1 data with the same DateTime my new select Top is
> equal to the previus sentence and i got the same data bunch.
> in order to evade this problem i want to create a routine on store
> procedure or any way on the server side to set different datetimes to
> all data...
> excuse my poor english... thanks in advance...!
> On 10 nov, 13:08, "Kart" <skarthike...@.bigfoot.com> wrote:
> > Hi,
> > I assume that you are asking about writing a procedure for this and not
> > a function. A function cannot update tables nor can it use getdate().
> > To do this in a procedure you can use the WAITFOR DELAY statement in
> > SQL.
> > Regards,
> > Kart
> > celerno wrote:
> > > hi, i need to create a function to do some like this...
> > > update myTable set DateModify = getdate()
> > > ... but... i need to get a minimum difference bettwen records...
> > > milisecons... or...
> > > how can i get an array or cursor (SQL server side) with 500 records
> > > with different datetime programatically?

generate real date time

hi, i need to create a function to do some like this...
update myTable set DateModify = getdate()
... but... i need to get a minimum difference bettwen records...
milisecons... or...
how can i get an array or cursor (SQL server side) with 500 records
with different datetime programatically?
Hi,
I assume that you are asking about writing a procedure for this and not
a function. A function cannot update tables nor can it use getdate().
To do this in a procedure you can use the WAITFOR DELAY statement in
SQL.
Regards,
Kart
celerno wrote:
> hi, i need to create a function to do some like this...
> update myTable set DateModify = getdate()
> ... but... i need to get a minimum difference bettwen records...
> milisecons... or...
> how can i get an array or cursor (SQL server side) with 500 records
> with different datetime programatically?
|||Hi Kart.. thanks for your response...
I try to use waitfor, but i can't find the way to do it.
sorry, i don't have good programming skills in sql server...
here is my problem:
i retrieve a bunch of data using the DateTime field to get the lastest
data using "select top N... ... where dateTimeNew>myDate"
after download the N rows, myDate gets the biggest dateTime value from
the N rows.
and when we have N+1 data with the same DateTime my new select Top is
equal to the previus sentence and i got the same data bunch.
in order to evade this problem i want to create a routine on store
procedure or any way on the server side to set different datetimes to
all data...
excuse my poor english... thanks in advance...!
On 10 nov, 13:08, "Kart" <skarthike...@.bigfoot.com> wrote:[vbcol=seagreen]
> Hi,
> I assume that you are asking about writing a procedure for this and not
> a function. A function cannot update tables nor can it use getdate().
> To do this in a procedure you can use the WAITFOR DELAY statement in
> SQL.
> Regards,
> Kart
> celerno wrote:
>
|||yeah, this query it all that i need... it was so simple.!
reference to:
http://www.sqlteam.com/item.asp?ItemID=765
declare @.intCounter datetime
set @.intCounter = getdate()
update articulo
SET @.intCounter = fechahora = dateadd(s, 1, @.intCounter)
select fechahora from articulo
On 11 nov, 11:06, "celerno" <cele...@.gmail.com> wrote:[vbcol=seagreen]
> Hi Kart.. thanks for your response...
> I try to use waitfor, but i can't find the way to do it.
> sorry, i don't have good programming skills in sql server...
> here is my problem:
> i retrieve a bunch of data using the DateTime field to get the lastest
> data using "select top N... ... where dateTimeNew>myDate"
> after download the N rows, myDate gets the biggest dateTime value from
> the N rows.
> and when we have N+1 data with the same DateTime my new select Top is
> equal to the previus sentence and i got the same data bunch.
> in order to evade this problem i want to create a routine on store
> procedure or any way on the server side to set different datetimes to
> all data...
> excuse my poor english... thanks in advance...!
> On 10 nov, 13:08, "Kart" <skarthike...@.bigfoot.com> wrote:
>
>
>
>