Can SMS generate "alter table" code when tables are beeing changed? - sql

In PHPMyAdmin there is an option to get changes to a database in sql code (and php if I remember correctly).
Is this possible on Microsofts platform?
If I for example have this table:
create table test (
id int,
text varchar(5)
)
and when I add a column, can I get the "alter table code" for it?

Yes.
Right-click on the table name in Management studio and select Design.
Make your changes
Before you save the changes, click the Table Designer menu and select Generate Change Script..
A window will appear which will have the SQL script.
If you don't want to save it to a file, you can select the text and copy it (Ctrl-A doesn't work unfortunately)

Related

SQL Server Designer table re-creation

Can I change the script the SSMS does when you use designer to alter your table which causes table re-creation?
I know the 'Prevent saving changes that require table re-creation' option but I want it to leave unchecked.
You can check this box to generate the script. From there, you can modify if you like.

How to find an object in all T-SQL scripts

I have an ERP Database and it is big. One of the table gets updated by an SP, TRIGGER, FUNCTION or something else. Because, I watched the Profiler to find UPDATE or INSERT statements but I couldn't find ant UPDATE or INSERT. Therefore, the table should be updated by SP, TRIGGER, FUNCTION or something else.
Is there a helper to find in which SP,FUNCTION OR TRIGGERS the table is used? I want to give the table name and it will tell me where the table name is used?
In SSMS do the following
Server->Database->Tables-> tablename ->right click -> view
dependencies
select Object that depends on radio button to view the object's that were using your table
Export all script objects to a file and search the file. You can do this from SQL Server Management Studio. Right Click the database and go to Tasks > Generate Scripts.
In SSMS you can right click a table and then choose 'view dependencies' or use sp_depends.

SQL drop table and re-create and keep data

On our original design we screwed up a foreign key constraint in our table. Now that the table is full of data we cannot change it without dropping all of the records in the table. The only solution I could think of is to create a backup table and put all of the records in there, then delete all the records, alter the table and start adding them back. Any other (BETTER) ideas? Thanks!
Using MS SQL Server
I'm a bit late, just for reference.
If You are using SQL Server Management Studio, You could generate a DROP and RECREATE script with "Keep schema and data" option.
Right click on the desired DB in object explorer
Tasks > Generate scripts
Select the table you want to script
Then clicking on Advanced button
"Script DROP and CREATE" ->"Script DROP and CREATE"
"Types of data to script" -> "Schema and data"
Hope this helps
Here's some pseudo-code. No need to make a backup table, just make a new table with the right constraint, insert your records into it, and rename.
CREATE TABLE MyTable_2
(...field definitions)
<add the constraint to MyTable_2>
INSERT INTO MyTable_2 (fields)
SELECT fields
FROM MyTable
DROP TABLE MyTable
exec sp_rename 'MyTable2', 'Mytable'
Using SQL Server Management Studio (SSMS), you can use it's table designer to specify the final condition of the table. Before saving the changes, have it generate the change script and save that script. Cancel out of the design window, open the script and review it. SSMS may already have generated a script that does everything you need, fixing the primary-foreign key relationship while preserving all existing data. If not, you will have a script, already started, that performs most of what you need to do and should be able to modify it for your needs.
This is your only solution.
Create the backup table, empty the original one, modify the table and then insert step-by-step until you find a violation.
Update All Schema Database Old by new Schema Database .
Create script (Right click on the desired DB in object explorer Tasks > Generate scripts -> select option select specific database objects and tables ->next -> advanced-> option Type of data to script Data only -> ok ->next ->next.) to data only and backup Database to old database
Drop database old and create new database and make new DB is empty .
Excute script of Old Data only on new database .

helptext for create table

Can I get the whole query which I used for creating a table, like we have sp_helptext to get the query of a stored procedure.
sp_helptext 'procedure_name'
Is there anything like this available for create table also in SQL server express?
I want to view the whole query which I wrote for creating a particular table and not the table structure.
Like if a deleted a table, and again want to create it, then I would have to type the whole query again, so I want a way through which I don't have to write the whole query again, like in mysql there is an option such as SHOW, which shows the table query?
In SQL Server Management Studio you can right-click on a table in the Object Explorer window and choose to generate the CREATE script into a new query window or put it in the clipboard or save it in a file.
Try sp_columns or sp_help. But this will not give you the CREATE TABLE text, you have to create this text for yourself.
You can also have a look at Catalog Stored Procedures

Why isn't SSMS smart when it comes to adding columns?

Whenever I want to add a column to a table it usually goes something like this:
Fire up SQL Server Management Studio (SSMS)
Select "Design" on the table I want to add the column to
Add the new column to the table
Save
Get an error that SSMS can't save because it would need to drop the table (and it can't because the the table has foreign keys on it).
Get frustrated that I forgot that this is something that SSMS can't do
Construct an alter table command by hand to add the column
Move on with life.
This time I am adding a step between numbers 6 and 7. I thought I would ask why SSMS can't make a simple alter table statement to add my new column in.
(In case it matters I am running SSMS 2008 against SQL Server 2008.)
Alternatively, you can go to Tools-->Options-->Designers-->Table and Database Designers and uncheck "Prevent saving changes that require table re-creation"
Problem solved.
Here's an explanation from MSDN: http://support.microsoft.com/default.aspx/kb/956176.
When you change a table so that you
alter the metadata structure of the
table, and then you save the table,
the table must be re-created based on
these changes.