Showing posts with label alter. Show all posts
Showing posts with label alter. Show all posts

Monday, March 26, 2012

Is it possible to generate alter Table statements using SMO

Hi

I'm trying to modify existing tables in a database.

How can I create alter Table scripts using SMO/DMO

Thank you

Yep, you can use the following to either execute and capture, just execute (which is the default) or just capture the executed commands:

Server s = new Server(".");

s.ConnectionContext.SqlExecutionModes = Microsoft.SqlServer.Management.Common.SqlExecutionModes.CaptureSql

//Microsoft.SqlServer.Management.Common.SqlExecutionModes.CaptureSql

//Microsoft.SqlServer.Management.Common.SqlExecutionModes.ExecuteAndCaptureSql

//Microsoft.SqlServer.Management.Common.SqlExecutionModes.ExecuteSql

//s.ConnectionContext.CapturedSql.Text; //Get the Text

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

Thanks again Jens

I'm trying the follwong code

Server server1 = new Server(".");

Database db= server1.Databases["master"];

server1.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;

foreach (Table Tbl in db.Tables)

{

tabl.Alter ();

}

db1.Refresh();

//writing to a file

writeToFile(server1.ConnectionContext.CapturedSql.Text, "alter", "tables");

But it is not generating Alter statments.

But if I use Create(), in place of alter(), it's generating Create statments.

|||Hi,
if you do not change anything, what are you supposed to see in the ALTER script :-) ?

In this sample I added a column to the table resulting in a script with an ALTER Script and an ADD column command.

Server s = new Server(".");

s.ConnectionContext.SqlExecutionModes = Microsoft.SqlServer.Management.Common.SqlExecutionModes.CaptureSql;

Table t = s.Databases["SMOTest"].Tables["TestTable"];

t.Columns.Add(new Column(t,"SomeSMOTest",DataType.DateTime));

t.Alter();

foreach (string st in s.ConnectionContext.CapturedSql.Text)

{

Console.WriteLine(st);

}

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

Thanks Jens

Got it.

while comparing a table in one database to other table (identical) in other database,

if the Source table has some modified(altered) columns and need to be modified in the target table.

How to solve this problem.Any Idea.

I generated the alter scripts manually for each column.

Like

ALTER TABLE [dbo].[wo]

ADD [requested-time] varchar (8 ) NULL

Thank you

|||You will have to do this manually. Load the two schemas and compare the columns (if you just want to check the columns) with each other. Change the columns appropiately with SMO and get the script from the Context. if you want an integrated tool which can do this on its own use Visual Studio for database professionals, this does have a comparer and script generator for keeping the databases in sync.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

Thanks alot Jens.

I'll try for this

Monday, March 19, 2012

Is it possible to alter snapshot agent to not drop tables?

We've got transactional replication set up from 2000 to 05. Is there a
way to not let the snapshot agent to drop and recreate the destination
tables, but rather truncate them?
Thank you,
Igor
*** Sent via Developersdex http://www.codecomments.com ***
Have a look at the article properties (@.pre_creation_cmd ). By default it is
Drop, but it can be converted to delete or truncate (or none).
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .
|||Right ON!
Thank you.
*** Sent via Developersdex http://www.codecomments.com ***

is it possible to alter a table to remove IDENTITY attribute

We have a column with an IDENTITY attribute and we no longer want this
column to have the IDENTITY attribute.
I know that set IDENTITY_INSERT will more or less ignore the IDENTITY for
that unit of work however I need the IDENTITY attribute totally removed so
that we don't have to set IDENTITY_INSERT each time.
Thanks in advanceYou can add a new column, copy the contents of the identity column to it,
drop the identity column, and rename the new column.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
"TJT" <TJT@.nospam.com> wrote in message
news:egxy3DYXGHA.1200@.TK2MSFTNGP03.phx.gbl...
We have a column with an IDENTITY attribute and we no longer want this
column to have the IDENTITY attribute.
I know that set IDENTITY_INSERT will more or less ignore the IDENTITY for
that unit of work however I need the IDENTITY attribute totally removed so
that we don't have to set IDENTITY_INSERT each time.
Thanks in advance

is it possible to alter a table to remove IDENTITY attribute

We have a column with an IDENTITY attribute and we no longer want this
column to have the IDENTITY attribute.
I know that set IDENTITY_INSERT will more or less ignore the IDENTITY for
that unit of work however I need the IDENTITY attribute totally removed so
that we don't have to set IDENTITY_INSERT each time.
Thanks in advanceYou can add a new column, copy the contents of the identity column to it,
drop the identity column, and rename the new column.
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
"TJT" <TJT@.nospam.com> wrote in message
news:egxy3DYXGHA.1200@.TK2MSFTNGP03.phx.gbl...
We have a column with an IDENTITY attribute and we no longer want this
column to have the IDENTITY attribute.
I know that set IDENTITY_INSERT will more or less ignore the IDENTITY for
that unit of work however I need the IDENTITY attribute totally removed so
that we don't have to set IDENTITY_INSERT each time.
Thanks in advance

Friday, February 24, 2012

is DB publication/subscription?

Because of problems trying to alter databases used
for replication my software would need to find out
if a database is a publisher or was repliated (using
T-SQL). Is this possible?Look in BOL for sp_dboption

This should be able to tell you if the database is Published etc

Cheers