I have a SQL Server table. I need to create the same table in another database.
How would I see what the Create Table query was that created the table so I can just run that.
CREATE TABLE ..
In SSMS you can right click the table and select Script Table as Create to New Query Window.
Let's assume SQL Server 2005 or greater, since you did not specify. In Management Studio, simply right-click on the table, select Script Table As --> Drop and Create --> To New Query Editor Window.
For SQL Server 2000, in Enterprise Manager, right-click the table and you'll have a similar option to script the table creation to a file. I forget the exact menu option text, but it's easy enough.
Related
In SQL Server Database Engine I have a table named Table A.
I deleted the table using graphical interface, but when I wanted to create a table with same name, the error shows
The object already exists
What is the remedy of this situation?
The following steps should help you track down what is going on and help you create your table:
Right-click on your database and select refresh
Verify that your table does not exist under this database.
If you table is
not shown here, then very likely your table is displayed under the
master database.
To create a table in your selected database,
first select the database and then run your query.
A better
option for number 4, just to be sure you are specifying the correct
database is to run the command use dbname; (where dbname is
the name of your database). Do this on the line above your create table code.
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)
We can update a table in designer view in Pl/sql by using following query:
select * from table1 for update
and than by unlocking the designer view and do further changes.
Can we do the same in SQL Server? I tried to do the same in SQL Server than I got an error:
FOR UPDATE clause allowed only for DECLARE CURSOR
Guys any ideas??
In Management Studio, right-click the table name in Object Explorer, and choose "Edit Top 200 Rows". If this does not return the row(s) you want to edit, hit Ctrl+3 and this will allow you to modify the query that populates the grid. In all honesty, you should be learning proper DML (update, insert, delete) syntax instead of treating your table like a spreadsheet.
You won't be able to change the schema from the same designer, though; for that you'll have to use right-click > Design. (And again, proper DDL is better than relying on bug-laden and feature-restricted visual designers.)
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 .
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