Showing posts with label generating. Show all posts
Showing posts with label generating. Show all posts

Wednesday, March 28, 2012

Scripter & foreign keys

Hey Peeps Smile

I'm having trouble generating scripts for my databases with SMO. Any foreign keys in the base will blow up the code below. The error message says that the target collumn of the foreign key does not exist, which is hogwash. I have tried this on 3-4 different bases with exact same result. I'm 100 million % sure that these db's and foreign keys are ok.

I cant believe MS has relased something with so obvious a problem, so it must be my fault. So my questions are.

1) What's wrong with the code below?
2) Lets says for arguments sake that the problem reported was true, why would SMO even care about that? Im only asking it to script what it finds, not to argue about consistency etc. (I tried with the DriCheck option to false with same result)

/cheers
/Frederic

Server server = new Server("localhost");
Scripter scripter = new Scripter(server);
Database database = new Database(server, "Test");

database.Refresh();

int objectCount = database.Tables.Count;

SqlSmoObject[] objectsToScript = new SqlSmoObject[objectCount];

for( int t = 0; t < objectCount; t++ )
{
objectsToScript[t] = database.Tables[t];
}

scripter.Options.DriForeignKeys = true;

StringCollection output = scripter.Script(objectsToScript);


Hi Frederic,

In the above code, you are using the statement,

Database database = new Database(server, "Test");

this is actually used to create a new database. What you need to do is access an existing database on the server. Use the statement

Database database = server.Databases["Test"];

This should solve your problem.

Thanks,

Kuntal

Scripter & foreign keys

Hey Peeps Smile

I'm having trouble generating scripts for my databases with SMO. Any foreign keys in the base will blow up the code below. The error message says that the target collumn of the foreign key does not exist, which is hogwash. I have tried this on 3-4 different bases with exact same result. I'm 100 million % sure that these db's and foreign keys are ok.

I cant believe MS has relased something with so obvious a problem, so it must be my fault. So my questions are.

1) What's wrong with the code below?
2) Lets says for arguments sake that the problem reported was true, why would SMO even care about that? Im only asking it to script what it finds, not to argue about consistency etc. (I tried with the DriCheck option to false with same result)

/cheers
/Frederic

Server server = new Server("localhost");
Scripter scripter = new Scripter(server);
Database database = new Database(server, "Test");

database.Refresh();

int objectCount = database.Tables.Count;

SqlSmoObject[] objectsToScript = new SqlSmoObject[objectCount];

for( int t = 0; t < objectCount; t++ )
{
objectsToScript[t] = database.Tables[t];
}

scripter.Options.DriForeignKeys = true;

StringCollection output = scripter.Script(objectsToScript);


Hi Frederic,

In the above code, you are using the statement,

Database database = new Database(server, "Test");

this is actually used to create a new database. What you need to do is access an existing database on the server. Use the statement

Database database = server.Databases["Test"];

This should solve your problem.

Thanks,

Kuntal

Monday, March 12, 2012

script formatting options in SQL 2005?

Where do I set the default Script Formatting options for generating the TSQL to create database objects? I know how to "right-click > Script As > Create To..."

What I need is to set the default scripting behavior to include a check for existence, drop the object if it exists, and also include object-level permissions. In SQL 2000 Query Analyzer, it was easy (Tools > Options > Script ). The result would look like:

if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[CustOrderHist]')
and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[CustOrderHist]
GO

CREATE PROCEDURE CustOrderHist @.CustomerID nchar(5)

AS
SELECT ProductName, Total=SUM(Quantity)
FROM Products P, [Order Details] OD, Orders O, Customers C
WHERE C.CustomerID = @.CustomerID
AND C.CustomerID = O.CustomerID

AND O.OrderID = OD.OrderID

AND OD.ProductID = P.ProductID
GROUP BY ProductName

GO

GRANTEXECUTEON [dbo].[CustOrderHist]TO [public]

GO

But in SQL 2005 Management Studio, I can't find the options to enable these features. The result of a "script procedure to new window" only has the CREATE statement, and I hate hand-coding the check for existence and permissions. The snippets highlighted in yellow above simply aren't included in the generated script. ?!?

Losing this capability will definitely be a huge issue in my shop, where we have about 15 devs and a couple of DBAs who are *very* accustomed to utilizing these "old features"...

Has anyone figured out how to get these back?

If you navigate through rightclick database > All Tasks > Generate Script you will have the full featured settings pane for tweaking your creation.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

We've added a dialog to configure scripting option in Service Pack 2. It will be accessed from the Tools > Options top-level menu item, then selecting the new "scripting" options page.

Thanks,
Steve

|||

Thanks, Jens.
Unfortunately, this is not going to help much. What we need is the ability to create "sticky" settings for the defaults, which would be used by the Right Click (the object) > Script To > (file, new window, or clipboard)... and use our preferred settings to generate the DDL with the desired formatting.

1. There is no apparent way to set the defaults to my custom settings, I have to go through the "wizard" every time and reset the options the way I need them. That takes about 20 mouse clicks. We need to be able to configure the defaults and make them sticky, like having them saved under the user profile, and recalled the next time without intervention.

2. The wizard really doesn't get us past the problem of first manually creating the DROP statements and then manually creating the CREATE statements. We'd have to run the wizard twice to get both sets of statements:
Under "General > Script Behavior", there are options to:
- Generate CREATE statements only
- Generate DROP statements only
... but there is no option to Generate DROP statements followed by CREATE statements, even though the tool indicates there *ought to be* such an option. Is this the result of some dependant option selection that I did or did not make?

3. The wizard does not appear to have any option for generating individual script files for each object. You can either create a new single file containing all the objects you've selected, OR append the current operation's script output to an existing file. We need separate files for each object. Not one enormous script for everything.

4. The wizard's check for object-level permissions can be too granular. I scripted a view, and was surprised by the outcome! It might be handy if you actually *want* to script out every permission on every column for every user and role, but most of the time we grant permissions to a role on the entire view or table, not every individual column. And we tend to combine the permissions granted onto a single statement per role, like this:

GRANT SELECT, INSERT, UPDATE, DELETE ON [dbo].[theView] TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] TO [theROLE_READONLY]

GO

This was the output from the wizzie:

GRANT SELECT ON [dbo].[theView] TO [theROLE]

GO

GRANT INSERT ON [dbo].[theView] TO [theROLE]

GO

GRANT DELETE ON [dbo].[theView] TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([idn]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([idn]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([idn]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([fk_idn]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([fk_idn]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([fk_idn]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([cre_usr_acct]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([cre_usr_acct]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([cre_usr_acct]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([lvl_idn]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([lvl_idn]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([lvl_idn]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([lst_mod_usr_acct]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([lst_mod_usr_acct]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([lst_mod_usr_acct]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([plnt_cde]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([plnt_cde]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([plnt_cde]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([ste_cde]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([ste_cde]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([ste_cde]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([ship_dte]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([ship_dte]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([ship_dte]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([orig_usr_acct]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([orig_usr_acct]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([orig_usr_acct]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([orig_print_dte]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([orig_print_dte]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([orig_print_dte]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([lst_print_usr]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([lst_print_usr]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([lst_print_usr]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([lst_print_dte]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([lst_print_dte]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([lst_print_dte]) TO [theROLE_READONLY]

GO

GRANT SELECT ON [dbo].[theView] ([cmnt]) TO [theROLE]

GO

GRANT UPDATE ON [dbo].[theView] ([cmnt]) TO [theROLE]

GO

GRANT SELECT ON [dbo].[theView] ([cmnt]) TO [theROLE_READONLY]

Do I have to write some kind of SQL Management Studio Add-In to get where I want to go? Or is there a registry hack or config file I can change (documented or not) to get closer to the desired end state?

Regards,
Rich

|||

Thanks, Steve. This is good news! When does SP2 come out? Will it be all-inclusive or additive with SP1? We haven't yet installed SP1.

|||Maybe this little tool will help in the meantime. I just commented out what I don't want and use a batch file to start the app.
http://www.elsasoft.org/tools.htm

script for generating service user account?

Hi,
Is there a script available to create the sql server service account and set
the file system and registry permissions correctly? I would like to run my
server with minimum access rights and I would like to save some time if
someone has done a nice script for creating the account and/or setting the
permissions in a way that it can easily be reused.
Svenhave a look at this
http://support.microsoft.com/kb/283811/en-us
this details what you need to script.
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Matzen" <Matzen@.discussions.microsoft.com> wrote in message
news:AA0493D1-A1F6-43F3-9D34-4A590FD410F8@.microsoft.com...
> Hi,
> Is there a script available to create the sql server service account and
> set
> the file system and registry permissions correctly? I would like to run my
> server with minimum access rights and I would like to save some time if
> someone has done a nice script for creating the account and/or setting the
> permissions in a way that it can easily be reused.
> Sven

script for generating service user account?

Hi,
Is there a script available to create the sql server service account and set
the file system and registry permissions correctly? I would like to run my
server with minimum access rights and I would like to save some time if
someone has done a nice script for creating the account and/or setting the
permissions in a way that it can easily be reused.
Svenhave a look at this
http://support.microsoft.com/kb/283811/en-us
this details what you need to script.
--
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Matzen" <Matzen@.discussions.microsoft.com> wrote in message
news:AA0493D1-A1F6-43F3-9D34-4A590FD410F8@.microsoft.com...
> Hi,
> Is there a script available to create the sql server service account and
> set
> the file system and registry permissions correctly? I would like to run my
> server with minimum access rights and I would like to save some time if
> someone has done a nice script for creating the account and/or setting the
> permissions in a way that it can easily be reused.
> Sven