SQL Server – sp_MSforeachdb – Undocumented Stored Procedure

The undocumented stored procedure “sp_MSforeachdb” is found in the “master” database. This is similar to sp_MSforeachtable. This stored procedure Sp_MSforeachdb gives a DBA the ability to cycle through every database in your catalog. This stored procedure will loop every database in your catalog for performing a command. This stored procedure accepts the following input parameters.   Example:  You can run a DBCC CHECKDB on all databases as below EXEC sp_Msforeachdb "DBCC checkdb ('?')" Similarly you can run other command also.

» Read more

SQL Server – sp_MSforeachtable – Undocumented Stored Procedure

As database administrators or developers, sometimes we need to perform an action on all of the tables within a database. For example, we may want to disable check constraints for all tables prior to loading a bunch of data. No doubt we can use the dynamic statement or cursor for this purpose. But we have better alternative way; this is an undocumented stored procedure called as “sp_MSforeachtable” in the master database. This stored procedure will loop through all the tables

» Read more

SQL Server – Local temporary table vs. Global temporary table

Temporary tables are a useful feature provided by SQL Server. Temporary tables created at runtime and can do all kinds of operations that one normal table can do. Because this is available at runtime, that’s why the scope of temporary tables is limited. These tables are created inside the tempdb database. There are two types of Temporary tables. Local Temporary table Global Temporary table Local temporary table: Local temporary tables are available to the current connection or sessions to the

» Read more

SQL Server – Database Read Only

Here is simple script with you can set your database in Read-Only Mode Once you specify database is in read-only mode then users can read data from the database, not modify it. Here is Script : Mark Database Read Only USE [master] GO ALTER DATABASE database_name SET READ_ONLY WITH NO_WAIT Similarly you can change database Read-Only Mode to Read-Write Here is script : Mark Data Read- Write Mode USE [master] GO ALTER DATABASE database_name SET READ_WRITE WITH NO_WAIT GO Alternative

» Read more

SQL Server – Management Studio – Add an External Tool to the Tools Menu

We can launch any Microsoft Windows or Microsoft .NET Framework application from SQL Server Management Studio. External applications can be added to, and run from, the Tools menu. To add an external tool to the Tools menu On the Tools menu, click External Tools. In the Title text box, type the name you want to appear in the Menu contents list. In the Command text box, type the program name. Include the path to the executable file if necessary. In the Arguments text

» Read more

SQL Server – Use of Cursors

I have seen many different reasons and point of view on the use of cursors. Some people never used, some used at last resort and some used regularly. In each of these, they must have different reasons for their stand on cursor usage. There must have a reason or place where we can use cursors in an efficient manner. Cursors are a bad choice, unless you understand enough about them to justify their use in limited circumstances. Cursors have their

» Read more

SQL Server – Import CSV file to SQL table – Bulk-Insert

Here is an example –  Import CSV file to SQL table using Bulk-Insert CSV file means  Comma Separated Values file. USE testdb GO CREATE TABLE table1 ( id INT, name VARCHAR(255), address VARCHAR(255) ) GO Create CSV file in Shared folder on any remote location with name csv.txt Suppose location of the file is \\dcc-566\Share\CSV.txt Content of CSV file as : Now let‘s Import this CSV file into Table1. BULK INSERT table1 FROM '\\dcc-566\Share\CSV.txt' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR

» Read more

SQL Server – Comma Separated INSERT Option

  Here is new feature of SQL Server 2008 – “Comma Separated INSERT Option” In this feature using a single INSERT statement you can pass multiple value for the table using comma separated option. USE testdb GO CREATE TABLE table1 ( id INT IDENTITY(1,1), name VARCHAR(255), address VARCHAR(255) ) SELECT * FROM testdb..table1 INSERT INTO testdb..table1 VALUES (‘Varinder’,’ABC Street’), (‘Amit’, ‘CDE Street’), (‘Dinesh’,’XYZ Street’) Result :    

» Read more

SQL Server – Insert same value multiple time in SQL table

  Here is an example:  Inserting same value multiple time in SQL table USE testdb GO CREATE TABLE table1 ( id INT IDENTITY(1,1), name VARCHAR(255), address VARCHAR(255) ) SELECT * FROM testdb..table1 INSERT INTO testdb..table1 VALUES (‘Varinder’,’ABC Street’) go 5 Result :  

» Read more