Friday, October 14, 2011

Open SSMS From Command Prompt

sqlwb command prompt utility which opens SQL Server Management Studio. sqlwb command does not run queries from command prompt. sqlcmd utility runs queries from command prompt, read for more information.

sqlwb
[scriptfile] [projectfile] [solutionfile]
[-S servername] [-d databasename] [-U username] [-P password]
[-E] [-nosplash] [-?]
I use following script very frequently.
1) Open SQL Server Management Studio.
sqlwb
2) Open SQL Server Management Studio with windows authentication with no splash screen and default database as AdventureWorks
sqlwb -E -S SQLServerName -D AdventureWorksnosplash
3) Open SQL Server Management Studio with username and password with no splash screen and default database as AdventureWorks
sqlwb -S SQLServerName -U MyUserName -P MyPassWord -D AdventureWorksnosplash
To execute above commands you should have access to sqlwb.exe file. My local computer has sqlwb file in folder : C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE

Click on Start >> click on run >> type in following command to launch SQL Server Management Studio :
“C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\sqlwb.exe” -E -S SQL -nosplash

Thursday, October 13, 2011

create Multiple tables with Same Name in a Database ?

The answer is YES .
We can create multiple tables with same name under Different Schema(s).
To Identify the Default schema:
Select SCHEMA_NAME() 'Default Schema'
Result is: dbo
Whenever we create a new table, The table will be created under "dbo" schema. Because this is the default schema.

Creating a Table:
Use SQLServerBuddy
Go
Create Table TB_Table1(ID Int Identity,Col1 Varchar(10))
Go
Result is: Command(s) completed successfully.

To Identify the table "TB_Table1" created under which schema
Use SQLServerBuddy
Go
select Object_schema_Name(object_id('[TB_Table1]'))
Result is: dbo

Let's try to Create the Table again:
Use SQLServerBuddy
Go
Create Table TB_Table1(ID Int Identity,Col1 Varchar(10))
Go
Result is:There is already an object named 'TB_Table1' in the database.
Because, The same table TB_Table1 trying to create under default schema "dbo", So the table already exists under the schema. Then how to proceed ?

Let's create a new schema named "SQLBuddy"
Use SQLServerBuddy
Go
CREATE SCHEMA [SQLBuddy]
Result is:Command(s) completed successfully.

Now, We can try to create the same table under "SQLBuddy" schema
Use SQLServerBuddy
Go
Create Table [SQLBuddy].[TB_Table1](ID Int Identity,Col1 Varchar(10))
Result is:Command(s) completed successfully.

Let's validate how many tables created with same name under which Schema(s)
Use SQLServerBuddy
Go
Select [Name],[schema_id] 'Schema ID',SCHEMA_NAME([schema_id]) 'Schema Name' from sys.tables Where [name] ='TB_Table1'

Result is:

Now, Two tables have been created with same name under different Schema(s) one table under "dbo" schema and one table under "SQLBuddy" schema in the same Database "SQLServerBuddy".

Note : SQLServerBuddy is the Database Name.

Conclusion: We can create more than one table with same name under different Schema in a Database.

Wednesday, October 5, 2011

INSERTED / DELETED tables in a SQL Server trigger

Triggers use two logical tables called the INSERTED and the DELETED table. These tables are accessed only inside triggers and you cannot directly modify the data in the tables. The tables hold the new and old rows of the data being inserted or deleted.

If you DELETE a row , a row will be created in the DELETED table. Similarly if you INSERT a row, a row is created in the INSERTED table. If you update a record with an UPDATE statement, the INSERTED table will contain the updated row and a previous state of the row will be added to the DELETED table.

Note: You cannot use a CREATE INDEX on these tables.