Show create table tablename in SQL Server - sql

In MySQL it is possible to do show create table tablename
What is the SQL Server equivalent?

In SSMS, right-click on the table node and "Script Table As" / "Create".
There is no built in 'script this table' T-SQL.
sp_help 'tablename' gives useful table information if that'd do.

One that might be close:
exec sp_columns YourTableName

if multiple database and schema exists in SQL_Dataserver,
Then you need to provide the exact table location with sp_help within single quotes.
exec sp_help 'database_name.schema_name.table_name'

Related

How can I alter column name in Sql server

I've a table Amount, having a column name amount_id which i want to change and update into account_id
I am using sp_rename function but i dont know how exactly i can change it
EXEC sp_rename 'Amount.Amount_id', 'Account_id', 'COLUMN';
This is the error given
Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.
You are using the syntax to rename a table. For rename a column you need
EXEC sp_rename '<Schema>.<Table>.<Fieldname>, 'newfield', 'COLUMN';
There are few possible ways of doing this in MSSQL server.
Select the table abn list the columns by click on the + in the table columns. Then right click and rename the column name
Go to the design of the table and then rename the column/ datatype, add new column, etc.
Use sp_rename system stored procedure to rename the table column name. this can be used to change the table name as well.
sp_rename 'table.column_name', 'new_columnName', 'COLUMN';
sp_rename 'Accounts.Marker', 'Markers', 'COLUMN';
The syntax mentioned above should work in SQL Server. If you are using MySQL, the following syntax should work:
ALTER TABLE tableName CHANGE `oldcolname` `newcolname` datatype(length);

Lost a table after sp_rename in SQL Server with unnecessary schema name in second parameter

I have executed the following SQL-script to rename a table:
EXEC sp_rename 'dbo.OriginalTable', 'dbo.TableWithNewName'
I know now this is incorrect and that the value of the second parameter should be TableWithNewName.
But now I can't find either of those tables and don't know how to fix this.
Use this script
SELECT * from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%Dbo.%' you will find the table which u renamed earlier. U can rename the table as it is previous
EXEC SP_RENAME '[dbo.TableWithNewName]','TableWithNewName'

How to redirect a request for DB1 to DB2

Suppose I have two databases named DB1 and DB2. In DB1, there is a table named Student, In DB2, there is a stored procedure named SP1. In SP1, I am selecting data of Student Table using below query :
Select *from DB1.dbo.Student.
I have more than 300 stored procedures having above said cross database communication. Now, I want to change my database from DB1 to DB3 that is identical to DB1 from data and schema perspective.
For this, I also have to modify all 300 stored procedures that are having fully-qualified database name. Now, the query will likely to be as follows :
Select *from DB3.dbo.Student
I don't want to change all stored procedure to point DB3 now, also don't want to change my queries written in stored procedure into dynamic SQL (I know this can be done by creating dynamic SQL).
Is it possible if We run DB1.dbo.Student, It will redirect to DB3.dbo.Student. Any intermediate layer or any SQL setting.
It'll be very big help for me. Thanks In Advance !!
If the purpose of your database renaming is to migrate a database, then why not rename the databases themselves?
e.g. say rename DB1 to DB1_old and then rename DB3 to DB1
I would simply script out all stored procedures using SQL Server script generator tool. Then do a find replace on the script and find text ‘DB1.dbo.’ and replace with ‘DB3.dbo.’
In the future you might want to consider using synonyms to reference external tables then you would only have to update the synonyms instead of all of your procedures. Please see following MSDN article on synonyms:
https://msdn.microsoft.com/en-us/library/ms187552.aspx
Example use of synonym:
USE [DB1]
GO
-- Create a synonym for table A located in DB2.
CREATE SYNONYM [dbo].[External_TableA] FOR [DB2].[dbo].[TableA]
GO
-- Synonym is pointing to TableA in DB2 , select statement will return data from DB2 tabla A.
SELECT *
FROM [External_TableA]
GO
-- Point the Synonym to same table but on DB3
DROP SYNONYM [dbo].[External_TableA]
CREATE SYNONYM [dbo].[External_TableA] FOR [DB3].[dbo].[TableA]
GO
-- No update was needed on views or stored procedure.
-- Synonym is pointing to TableA in DB3 , select statement will return data from DB3 tabla A.
SELECT *
FROM [External_TableA]
The follow query will generate the required DROP and CREATE script to remap your synonyms from the old database to the new database.
DECLARE #oldDB NVARCHAR(100) = 'DB2';
DECLARE #newDB NVARCHAR(100) = 'DB3';
SELECT 'DROP SYNONYM [dbo].[' + name + ']' AS [Drop Script]
,'CREATE SYNONYM [dbo].[' + name + '] FOR ' + REPLACE(base_object_name, #oldDB, #newDB) AS CreateScript
FROM sys.synonyms
ORDER BY name
its better to use USE Keyword
use [database name you want to access]
Queries and stored procedure you want to use
GO
eg
use [db1]
select *from yourTableName
exec yourStoredProcedure parm1,parm2,....
Go

Executing Dynamic SQL Using A Results Set

I'm using SQL Server and I want to construct a dynamic SQL statement. I have several databases that are exact clones of each other e.g. TestDatabase1 is the same as TestDatabase2 and etc. Since the schemas and tables in all of the cloned databases are exactly the same, I want to execute a SQL statement that updates each table. Here's the pseudo-code:
for each table x in a test database
update x.SomeColumn
I have code to grab the databases:
SELECT name
FROM sys.databases
WHERE name LIKE '%Test%'
but now I don't know what to do with that data. How can I update each table in each database?
You can use sp_MSforeachtable to run Update statements against every table in a database. For example:
-- First set the database you want to use:
USE TempDatabase1
GO
EXEC sp_MSforeachtable 'UPDATE ? SET SomeColumn = 2'
GO
USE TempDatabase2
GO
EXEC sp_MSforeachtable 'UPDATE ? SET SomeColumn = 2'
GO

How to change a table name using an SQL query?

How can I in change the table name using a query statement?
I used the following syntax but I couldn't find the rename keyword in SQL server 2005.
Alter table Stu_Table rename to Stu_Table_10
Use sp_rename:
EXEC sp_rename 'Stu_Table', 'Stu_Table_10'
You can find documentation on this procedure on MSDN.
If you need to include a schema name, this can only be included in the first parameter (that is, this cannot be used to move a table from one schema to another). So, for example, this is valid:
EXEC sp_rename 'myschema.Stu_Table', 'Stu_Table_10'
In MySQL :-
RENAME TABLE `Stu Table` TO `Stu Table_10`
In Postgress SQL:
Alter table student rename to student_details;
Please use this on SQL Server 2005:
sp_rename old_table_name , new_table_name
it will give you:
Caution: Changing any part of an object name could break scripts and
stored procedures.
but your table name will be changed.
In MySQL :
RENAME TABLE template_function TO business_function;
ALTER TABLE table_name RENAME TO new_table_name;
works in MySQL as well.
Alternatively:
RENAME TABLE table_name TO new_table_name;
Syntex for latest MySQL versions has been changed.
So try RENAME command without SINGLE QUOTES in table names.
RENAME TABLE old_name_of_table TO new_name_of_table;
RENAME TABLE old_table_name TO new_table_name;
rename table name :
RENAME TABLE old_tableName TO new_tableName;
for example:
RENAME TABLE company_name TO company_master;
execute this command
sp_rename 'Employee','EData'