Automatic "Script database as... create" for SQL Server 2008 - tsql

I'd like to make a daily automatic creation of the creation script for a database in SQL Server 2008, meaning having a (bat, exe, ... whatever) that creates a text file with the very same output obtained by right-clicking on the database in SQL Management Studio and do Script database as... --> Create to... --> New Query window.
Any hint?
Thanks in advance,
Mauro
EDIT 1: We already used SMO but a couple of things are annoying
Needs compatibility pack in SQL Server 2008 (confirm?)
Some things were not created in script (some indexes and keys and in table creation schema is lost)
Best choice for me would something with a powershell script but any other idea is welcome!

You can use SMO. For example: http://www.sqlteam.com/article/scripting-database-objects-using-smo-updated (see the Scripting Objects section).
More info on SMO: http://msdn.microsoft.com/en-us/library/ms162169.aspx

Related

SQL Server 2012 database stored and need to be restored on SQL Server 2008R2

As the title says, I need to restore a SQL Server 2012 database (express) to a SQL Server 2008 R2 production database.
I cannot find a way to do so.
The scripting seems to fail due to the size of one of the tables having about 300.000 records.
Any way to "downgrade" the 2012 database?
Thanks
Michael
You CANNOT do this - you cannot attach/detach or backup/restore a database from a newer version of SQL Server (2012) down to an older version (2008 R2) - the internal file structures are just too different to support backwards compatibility. There's no way, no trick, no hack, no magic to make this happen. Period.
You can either get around this problem by
using the same version of SQL Server on all your machines - then you can easily backup/restore databases between instances
otherwise you can create the database scripts for both structure (tables, view, stored procedures etc.) and for contents (the actual data contained in the tables) either in SQL Server Management Studio (Tasks > Generate Scripts) or using a third-party tool
or you can use a third-party tool like Red-Gate's SQL Compare and SQL Data Compare to do "diffing" between your source and target, generate update scripts from those differences, and then execute those scripts on the target platform; this works across different SQL Server versions.

How to Restore data from MS SQL 2012 to MS SQL 2008 using .bak file. Is this Possible?

I have a requirement that am facing difficulty i.e. restoring a data from MS SQL 2012 to MS SQL 2008 using .bak file. when am trying to restore , am getting an error. is this possibility to restore this to downgrade.
can any one help me how to restore ?
Unfortunately SQL Server will not go backwards. The only way you can do this is Using SSIS to transfer the data or you can use the import/export utility.
On your 2012 Server Right click on the DB you want to export data from and scroll down to tasks and then choose export data it will have you then insert all of the information for both the source and destination servers then on the summary page click mappings and make sure they are correct for what you need and the DB it is going to is correct. Then follow the screens until you can click finish. YOu may have too play with it for a little bit to get it to behave nicely but this should work for you.
If all you have is a BAK file you may have to resotre it to a dummy DB on the 2012 server first.

How to export data from SQL Server to PostgreSQL?

I need to export all tables from SQL Server to PostgreSQL.
Try: I tried from SQL Server IDE but at some stage its giving the error about data types are different.
Question:How can I do export of data from SQL Server to PostgreSQL? Is COPY does my job? If yes, then how can I export all tables including records?
You can't export data from MSsql then import to PostgreSql because it is not same syntax, data type, but you can use tool to migration data from mssql to postgreSql,
See more in topic
migrate data from MS SQL to PostgreSQL?
Use https://dbeaver.io/
Create MS SQL and PostgreSQL database connections (login)
Create target tables in PostgreSQL (same structures in MS SQL)
F5 to see new tables
Right-click on new tables -> 'Import Data' -> You will see 'Data Transfer' window
Choose 'Table' type then click 'Next' -> You will see 'Select input object', where you can choose tables from MS SQL connection
Just 'next' and check settings that you need, done :D
First export the schema into a file and run it against PostgreSQL until you've removed all incompatibilities.
You could try to do the same with the data you want to export but you may be better off writing a Python script to migrate it.
There is an absolutely simple way using built-in SSIS tool using Management Studio. You can find the detailed answer here.
Use https://dbeaver.io/ , as An Le mentioned.
After 40 years of DB development, migrating DB data is still a challenge. DBeaver is a free tool to use for data migration. But you still have to migrate the schema.
Exporting data from DBeaver
From contextual menu of your SQLServer database or schema select Tools > Create new Task > Common > Data Export
You will generate SQL insert files or CSV files. For migration between database types use CSV files.
Cons of SQL Server Migration Tool
Unable to migrate rows containing booleans.
Export ended up in errors of migrationg data with Bool columns, complaining that value is not boolean, although both source and destination columns where of boolean type.
Unable to continue with the next tables afer one table migration fails.
SQL Server - A single error stops all migration even for tables that are not related to the initial error.
Configuring the tool over and over again, trying to export your data is a waste of time. SQL Server migration task does not save the configuration of the source and destination connections. And the wizard is not user friendly, spending your time on it is frustrating. I assume the migration project was abandoned for at least 10 years.

Rename SQL Azure database?

How can i rename the database in sql Azure?
I have tried Alter database old_name {MODIFY NAME = new_name} but not worked.
Is this feature available in SQL Azure or not?
Just so people don't have to search through the comments to find this... Use:
ALTER DATABASE [dbname] MODIFY NAME = [newdbname]
(Make sure you include the square brackets around both database names.)
Please check that you've connected to master database and you not trying to rename system database.
Please find more info here: https://msdn.microsoft.com/en-US/library/ms345378.aspx
You can also connect with SQL Server Management Studio and rename it in Object Explorer. I just did so and the Azure Portal reflected the change immediately.
Do this by clicking on the database name (as the rename option from the dropdown will be greyed out)
Connect with SQL Server Management Studio to your Azure database server, right-click on the master database and select 'New Query'. In the New Query window that will open type ALTER DATABASE [dbname] MODIFY NAME = [newdbname].
It's Very simple for now - Connect to DB via SQL Management Studio and Just rename as you generally doing for DB [Press F2 on DB name]. It will allow you to do this and it will immediately reflect the same.
I can confirm the
ALTER DATABASE [oldname] MODIFY NAME = [newname];
works without connecting to master first BUT if you are renaming a restored Azure database; don't miss the space before the final hyphen
ALTER DATABASE [oldname_2017-04-23T09 -17Z] MODIFY NAME = [newname];
And be prepared for a confusing error message in the Visual Studio 2017 Message window when executing the ALTER command
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
You can easily do it from SQL Server Management Studio, Even from the community edition.

how to see/script definitions of system views?

I tried to execute scripts from [1] in model database and user-defined databases but they give definitions/scripts of only user-defined (non-system) views, i.e. those that I anyway can easily get from GUI.
How can I see/script the definition/script of a system view in SQL Server 2008 R2?
[1]
Answers to question "System Views text in SQL Server 2005"
System Views text in SQL Server 2005
select object_definition(object_id('[sys].[server_permissions]'))
AS [processing-instruction(x)] FOR XML PATH('')
(The XML bit is just to prevent long definitions getting truncated when viewed in SSMS)
You can also stop the SQL Server instance. Copy the mssqlsystemresource file (on my system this is at C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\Binn\mssqlsystemresource.mdf and then re-attatch the copy under a new name).
In the reattached version it is easier to poke around and see the various definitions using normal SSMS functionality.