Thursday, March 31, 2016

Generate Sequence number in column

1st Way
alter table Example
add NewColumn int identity(1,1)

 
2nd Way
DECLARE @id INT

SET @id = 0

UPDATE accounts2
SET @id = id = @id + 1
GO

  





Monday, March 28, 2016

Comma Separated string into individual rows









SELECT A.[State], 
     Split.a.value('.', 'VARCHAR(100)') AS String 
 FROM  (SELECT [State], 
         CAST ('<M>' + REPLACE([City], ',', '</M><M>') + '</M>' AS XML) AS String 
     FROM  TableA) AS A CROSS APPLY String.nodes ('/M') AS Split(a);




Friday, January 1, 2016

Get a List of Tables from database having specified column.





SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
    JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%ColumnName%'

OR

SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%ColumnName%'

How to Rename a Column Name or Table Name

The script for renaming any column :
sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'
The script for renaming any object (table, sp etc) :
sp_RENAME '[OldTableName]' , '[NewTableName]'
This article demonstrates two examples of renaming database object.
  1. Renaming database table column to new name.
  2. Renaming database table to new name.
In both the cases we will first see existing table. Rename the object. Test object again with new name.


1. Renaming database table column to new name.
Example uses AdventureWorks database. A small table with name “Table_First” is created. Table has two fields ID and Name.




















Now, to change the Column Name from “Name” to “NameChange” we can use command:
USE AdventureWorks
GO
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'
GO

Following Fig. show use of SP_RENAME Command










You can see the column name “Name” is now changed to “NameChange“.
USE AdventureWorks
GO
SELECT *
FROM Table_First
GO

Following fig. verify that the column name has been changed.














2.Renaming database table to new name.
We can change the table name too with the same command.
sp_RENAME 'Table_First', 'Table_Last'
GO
Following fig. Shows how we can change Table Name.














 Now, the table name “Table_First” is renamed as “Table_Last”.
“Table_First” will no longer be available in database. We can verify this by running script:
USE AdventureWorks
GO
SELECT *
FROM Table_First
GO
The Messages shows an error “Invalid object name ‘Table_First’.”
To check that the new renamed table exist in database run script:
USE AdventureWorks
GO
SELECT *
FROM Table_Last
GO



















 You can see the same data now available in new table named “Table_Last”



Thursday, December 31, 2015

Make SQL select statement to get same row multiple times

If I get your meaning then a very simple way is to cross join on a derived query on a table with more than 1000 rows in it and put a top 1000 on that. This would duplicate your results 1000 times.



SELECT
    MyTable.*
FROM
    MyTable
CROSS JOIN
(
    SELECT TOP 1000
        *
    FROM
        sysobjects
) [BigTable]
WHERE
    MyTable.ID = 1234