Add IDENTITY or Unique number to a table – T/SQL

I want to update my table records. However, my existing table does not have any unique columns. So, I need to append Identity column and update all the records based on that Identity column. For example, If my temp table has 1000 records without any unique column values. I need to number all these 1000 records and update the values. There is no need to do an UPDATE. The identity column is going to be populated when it is created….

Read More

MSSQL – Query to Find Column From All Tables of Database

How many tables in the database AdventureWorks have column names like ‘EmployeeID’? It was quite an interesting question and I thought if there are scripts that can do this would be great. I quickly wrote down the following script which will go return all the tables containing specific columns along with their schema name.

Read More

SQL Time Zone Conversion Functions

I did a lot of research on Microsoft’s website, and the only time zone conversion functions that I could find were relative to analysis services, and would not provide me with a solution to my problem. So, my options were to pay someone to modify our application, or create a process in the database environment to perform the conversions as data is inserted into the common database environment.

Read More

How can I speed up queries on a large table?

Any process or action that requires querying the database and getting results can be optimized by adding database indexes. An index is used to speed up the performance of queries. It does this by reducing the number of database data pages that must be visited/scanned. In SQL Server, a clustered index determines the physical order of data in a table. There can be only one clustered index per table (the clustered index IS the table) What is an index?So,…

Read More

How to Change Schema of Mssql Tables, Stored Procedures, and Views All at the Same Time

Sometimes changing database users changes who the database owner is for a table from dbo.tablename to badschema.tablename. This will also affect any stored procedures created under the tables with the incorect schema.  If you need to change the schema for these tables back to the default dbo schema follow the steps below. Open Microsoft SQL Server Management Studio and log in. Click the New Query button. Paste the following script into the New Query box changing oldschema to the name of the current schema:…

Read More

Find Duplicate Values with SQL Script

Let’s assume that data has been entered in a table and now you want to find if a table column have duplicated values. Here is an SQL script to join data from one table for comparison purpose. The table must have a primary key column (unique column) and the second column for comparison. Here is a sample script: Details: – DWDOCID is the primary key column – AIXFlag, is just a fixed value (optional) – SAMPLEEN is the table…

Read More