Archive
SQL Server – How to Move Table to Another Schema
Starting with SQL Server 2005 all tables are grouped into schemas. While creating a table if the schema name is not specified it is created in the default schema of the user creating it. you can use ALTER SCHEMA command to move tables between schemas.
For example, if I create a table using below script it will be created under my default schema which is dbo:
USE [SqlAndMe]
GO
CREATE TABLE Employee
(
ID INT,
Name VARCHAR(20)
)
GO
SELECT name, [schema] = SCHEMA_NAME(schema_id)
FROM sys.tables
WHERE name = 'Employee'
GO
Result Set:
name schema
Employee dbo
(1 row(s) affected)
As you can see from the output the table is currently in dbo schema.
Now to move this table to another schema using ALTER SCHEMA command, first we need to create the schema if it does not exist already. After that we can move table to new schema.
USE [SqlAndMe]
GO
CREATE SCHEMA HumanResources
GO
ALTER SCHEMA HumanResources
TRANSFER dbo.Employee
GO
SELECT name, [schema] = SCHEMA_NAME(schema_id)
FROM sys.tables
WHERE name = 'Employee'
GO
Result Set:
name schema
Employee HumanResources
(1 row(s) affected)
As you can see from the output the Employee table is now moved to HumanResources schema.
Hope This Helps!
Vishal
If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe
SQL Server – How to Rename Table/Column in SQL Server
To rename an object in SQL Server you can use sp_rename system stored procedure or you can do this via Object Explorer in SQL Server Management Studio.
Method 1: To rename an object using SQL Server Management Studio:
Step 1. Right Click on the object in Object Explorer and select "Rename"
Step 2. Specify new name and press Enter.
You can rename any object using object in Object Explorer.
Method 2: Renaming an object using sp_rename:
You can also rename using sp_rename system stored procedure.
sp_rename takes below arguments:
| Parameter | Description |
| @objname | Object Name. When renaming a column you need to specify table name.column name optionally you can also prefix schema name |
| @newname | New name for the specified object |
| @objtype | Type of the object. You can rename below objects using sp_rename: COLUMN DATABASE INDEX OBJECT STATISTICS USERDATATYPE Default value for this parameter is TABLE |
Following example demonstrates how you can use sp_rename to rename table and column names:
Let’s create a Test Table to work with:
USE [SqlAndMe]
GO
CREATE TABLE tblProduct
(
ID INT,
pName NVARCHAR(50)
)
GO
Now we can rename the Table Name and column names as below:
– Rename table tblProduct to Table_Products
EXEC sp_rename 'tblProduct', 'Table_Products'
GO
– Rename Column ID to ProductID
EXEC sp_rename 'Table_Products.ID', 'ProductID', 'COLUMN'
GO
– Rename Column pName to ProductName
EXEC sp_rename 'Table_Products.pName', 'ProductName', 'COLUMN'
GO
Result Set:
Caution: Changing any part of an object name could break scripts and stored procedures.
Caution: Changing any part of an object name could break scripts and stored procedures.
Caution: Changing any part of an object name could break scripts and stored procedures.
You can verify that the changes have been made by issuing a SELECT against the table using new table name. SQL Server is also nice enough to warn us that renaming an object can break scripts and stored procedures. What this means is if you have used table/column names in any script/stored procedure it will not be updated automatically, you will need to go through your scripts/stored procedures manually and update them accordingly.
Hope This Helps!
Vishal
If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe
SQL Server – DDL vs. DML Triggers
Although, DML and DDL triggers are used for different purposes, there are some similarities and differences between these.
Similarities between DDL and DML triggers:
1. Both are created using similar T-SQL syntax
2. Both can run .NET code
3. You can create multiple DDL and/or DML triggers on an object
4. The trigger and the triggering statement run as part of the same transaction
5. Both type of triggers can be nested
Differences between DDL and DML triggers:
1. There are no INSTEAD OF DDL triggers, they are only executed after the triggering statement is completed.
2. DML triggers use virtual tables INSERTED and DELETED which captures data modification, DDL triggers do not have this
3. DDL triggers captures information about the event that fired them using EVENTDATA() function.
Hope This Helps!
Vishal
If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe
Importing data from Excel – using INSERT statements
You can easily import data from an Excel file to SQL Server using SQL Server Import and Export Wizard. However, when the data is simple and limited I avoid using it (too lazy to click through 6 screens…
) instead I generate INSERT statements using CONCATENATE function in Excel and execute those. This is much faster than using Import/Export Wizard.
This approach also requires the destination table to be created manually or it should already exists. Here’s how I use it:
1. The destination table I am using already exists with below columns, and some data:
2. The source Excel file contains below data:
3. Now to convert this to INSERT statement, we need to add a few columns to Excel sheet which will contain these texts "INSERT INTO dbo.ProductList VALUES (' ", " '' "," ' " etc. as shown below:
Note: if single quote (') is the first character in the column, as in Column C; you will need to input two single quotes (''). Also, if table has additional columns which are not being imported then you will need to include column list with INSERT statement i.e. "INSERT INTO dbo.ProductList (Name) VALUES (' " if you are importing only Name column.
4. Then you need to concatenate these columns using Excel's CONCATENATE function:
5. Finally, copy the same formula to all rows, these are the INSERT statements you need!:
6. That's all folks, Now copy them to SSMS and execute them…
Hope This Helps!
Vishal
If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe
Exporting data to CSV file using SQLCMD/BCP
When it comes to exporting data there are different options available in SQL Server, such as SQL Server Integration Services, Generating Insert Scripts, BCP, Import and Export Wizard and SQLCMD.
When it comes to exporting data as CSV file, BCP and SQLCMD are the easiest methods. Both are command-line tools which can be used to export data.
General syntax for SQLCMD is as follows:
SQLCMD -S <<ServerName>> -E -Q "EXEC AdventureWorks2008R2.dbo.uspExport"
-s "," -o "C:\DataSqlCmd.csv" -h-1
Where, -S specifies Database Server Name,
-E specifies to use Windows Authentication,
-Q specifies the query to be executed against server,
-s specifies column separator character,
-o specifies the destination file and,
-h-1 specifies that column headers should not be exported.
You can provide a SELECT statement or a Stored Procedure to -Q switch. In general I use stored procedures as it allows to do any formatting required which cannot be done using SQLCMD.
I have created the stored procedure dbo.uspExport as below:
USE AdventureWorks2008R2
CREATE PROCEDURE uspExport
AS
SET NOCOUNT ON
SELECT TOP 5 BusinessEntityID,
JobTitle,
Gender,
BirthDate
FROM HumanResources.Employee
The stored procedure simply returns top5 rows which are to be exported.
Executing the above SQLCMD command will generate the CSV file as follows:
1,Chief Executive Officer ,M, 1963-03-02
2,Vice President of Engineering ,F, 1965-09-01
3,Engineering Manager ,M, 1968-12-13
4,Senior Tool Designer ,M, 1969-01-23
5,Design Engineer ,F, 1946-10-29
Same data can be exported using BCP as follows:
bcp "EXEC AdventureWorks2008R2.dbo.uspExport" queryout "C:\DataBCP.csv" -c -t , -S (local) -T
Here, -c specifies the character type format,
-t specifies the column separator,
-S specifies the database server name and,
-T specifies to use Windows Authentication.
The output generated by BCP is slightly different than of SQLCMD, output generated by BCP is as follows:
1,Chief Executive Officer,M,1963-03-02
2,Vice President of Engineering,F,1965-09-01
3,Engineering Manager,M,1968-12-13
4,Senior Tool Designer,M,1969-01-23
5,Design Engineer,F,1946-10-29
You can see that there is no padding while exporting using BCP by default, same can be achieved by using -W switch with SQLCMD.
If you need to implement any "complex" formatting, such as using delimiter for starting and end of row or change data format you can do this inside the stored procedure created for exporting data.
USE AdventureWorks2008R2
CREATE PROCEDURE uspExport
AS
SET NOCOUNT ON
SELECT TOP 5 '#' + CAST(BusinessEntityID AS NVARCHAR(MAX)) + '#' +
CAST(JobTitle AS NVARCHAR(MAX)) + '#' +
CAST(Gender AS NVARCHAR(MAX)) + '#' +
CONVERT(NVARCHAR(20), BirthDate, 107) + '#'
FROM HumanResources.Employee
Here I have returned result as a single column with specifying ‘#’ as a delimiter and to denote start/end of a row. So all rows returned by stored procedure will be pre-formatted and -s or -t switches of SQLCMD/BCP will have no effect.
#1#Chief Executive Officer#M#Mar 02, 1963#
#2#Vice President of Engineering#F#Sep 01, 1965#
#3#Engineering Manager#M#Dec 13, 1968#
#4#Senior Tool Designer#M#Jan 23, 1969#
#5#Design Engineer#F#Oct 29, 1946#
To see all switches available for BCP/SQLCMD refer BOL links or execute them with -? at command prompt.
Hope This Helps!
Vishal
If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe

