Get Session ID

To get the session ID, simply use the intrinsic variable @@SPID: [cc lang=”sql”] SELECT @@SPID [/cc] The acronym for SPID means Server Process ID. It is synonymous with session. Continue reading ...

How to Copy a Table

There are few methods of copying a table to another database, depending on your situation. Same SQL Server Instance If trying to copy a table to a database that is on the same instance of SQL Server, The easiest solution is to use a SELECT INTO while using the fully qualifed database names. [cc lang=”sql”] SELECT * INTO Database2.dbo.TargetTable FROM Database1.dbo.SourceTable [/cc] This will create a copy of the entire table across two different databases and will definitely be the quickest way. This method utilizes minimal inserts. The caveat is that it will not copy over the primary keys, foreign keys or the indexes. These will have to be scripted out and added after the insert. Different SQL Server Instance (and/or different server) If trying to copy a table to a different instance (be it on the same server or not), there are two basic routes. OPENROWSET is an easy way to do the insert. Just replace the server names below and instance name (if the database is defined by an instance). [cc lang=”sql”] SELECT * INTO dbo.TargetTable FROM OPENROWSET(‘SQLNCLI’, ‘server=SourceServerNameSourceInstance;trusted_connection=yes’, ‘SELECT * FROM SourceDatabase.dbo.SourceTable’) [/cc] The other way is to utilize the SSIS Import/Export Wizard. This always works well too. In order to initialize, right click on the database, select tasks, then import/export.

SELECT TOP 1

There are many good uses of the SELECT TOP 1 method of querying. Essentially, the select top 1 method is used to find the min or max record for a particular column’s value. There is some debate as to whether this is the ‘correct’ method of querying, however it should be known that this method does not break any guidelines and is supported by all standards of SQL. The TOP 1 means to only return one record as the result set. which record is returned, depends on the column that is specified in the order by clause. If you want to find the record with the minimum value for a particular column, you would query the record with the ORDER BY being ascending (ASC). If you want to find the maximum record with that value, you would query it with the ORDER BY descending (DESC). For example, say you wanted to find the record in a table for a customer that has the largest order. Traditionally, the way to do this would be to find the maximum order value then join back on the table in order to find the record. Like the following: [cc lang=”sql”] SELECT * FROM SalesLT.SalesOrderHeader soh WHERE TotalDue = ( SELECT MAX(TotalDue) FROM SalesLT.SalesOrderHeader WHERE SalesOrderID = soh.SalesOrderID ) [/cc] However, using the SELECT..TOP 1 method of querying, you can perform the same query using the following notation. [cc lang=”sql”] SELECT TOP 1 * FROM SalesLT.SalesOrderHeader ORDER BY TotalDue DESC [/cc] One of the major […] Continue reading ...

Check if Database Exists

In creating a database you also need to check whether or not the database already exists. In order to do so, simply use the ‘if exists’ method and select the name of the database from sysdatabases. [cc lang=”sql”] IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N’SQLServerPlanet’) CREATE DATABASE [SQLServerPlanet] [/cc] The code below will drop an existing database if it exists so be careful. [cc lang=”sql”] IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N’SQLServerPlanet’) DROP DATABASE [SQLServerPlanet] [/cc] An alternate method is to use the db_id to convert the db_name and see if it is null. [cc lang=”sql”] if db_id(‘SQLServerPlanet’) is not null BEGIN SELECT ‘database does exist’ END ELSE BEGIN select ‘database does not exist’ END [/cc]

String or binary data would be truncated.

This error message is relatively straight forward. The way it normally happens is when you are trying to insert data from a table that contains values that have larger data lengths than the table you are trying to insert into. An example of this would be trying to insert data from a permanent table, into a temporary table that has data types defined at a fixed length. In order to illustrate the example, we’ll create two temporary tables below and try to insert a value that will not fit into the second temporary table. [cc lang=”sql”] — First create the table that will hold the initial value CREATE TABLE #Employee ( ID int IDENTITY(1,1) ,FirstName varchar(50) ,LastName varchar(50) ) — Insert the example value INSERT INTO #Employee ( FirstName ,LastName ) SELECT FirstName = ‘Billie’ — 6 characters ,LastName = ‘Jean’ — Create the smaller table that will not fit the value CREATE TABLE #Employee_Truncate ( ID int ,FirstName varchar(5) — 5 characters ,LastName varchar(49) ) — Attempt to fit the smaller value into the table INSERT INTO #Employee_Truncate ( FirstName ,LastName ) SELECT FirstName ,LastName FROM #Employee [/cc] From this we see the following error: In order to fix the error, either the second temp table needs to have its data type expanded to be able to fit the data, or you could use the SELECT..INTO method of inserting the data. That way, no matter what size of data the source table changed to, the destination table would automatically […] Continue reading ...

Featured Articles

 Site Author

  • Thanks for visiting!
css.php