How To Create a Comma Separated List Of Table Columns Using T-SQL
TODO:Have you ever wanted to get a comma separated list of columns that belong to a table using T-Sql? SOLUTION:DECLARE @columnList varchar(8000) DECLARE @name varchar(100) DECLARE db_cursor CURSOR FOR...
View ArticleHow To Find The Tables And Columns Referenced By A Foreign Key Using T-SQL
TODO:Have you ever wanted to find the table and column names referenced in a Foreign Key Constraint using T-SQL? SOLUTION:SELECT OBJECT_NAME(fk.parent_object_id) TableName,...
View ArticleHow To Incrementally Shrink A SQL Server Database Using T-SQL
TODO:Have you ever wanted to shrink a large SQL Server database in small increments? SOLUTION:declare @DBFileName sysname declare @TargetFreeMB int declare @ShrinkIncrementMB int -- Set Name of...
View ArticleHow To Use a CASE Statement In T-SQL
TODO:Have you ever wanted to use a CASE statement in your T-SQL Select Query? SOLUTION:SELECT Column = CASE WHEN SomeValue = 0 THEN 'Zero' WHEN SomeValue < 0 THEN 'Minus' ELSE 'Plus' END FROM...
View ArticleHow To Update From Using T-SQL
TODO:Have you ever wanted to perform a join during an Update using T-SQL? SOLUTION:UPDATE Person SET Person.Name = PersonArchive.Name FROM Person INNER JOIN PersonArchive ON Person.Id =...
View ArticleHow To List All Columns In All User Tables In A Sql Server Database
TODO:Have you ever wanted to list all columns in all User tables that exist in a SQL Server Database? SOLUTION:SELECT so.name "table", sc.name "column", sm.text "Default" FROM dbo.sysobjects so INNER...
View ArticleHow To Restore A SQL Server Database Master Key On A New Server
TODO:Have you ever wanted to backup your database master key and move it to a new server? SOLUTION:--Step 1 -- Backup file OPEN MASTER KEY DECRYPTION BY PASSWORD 'my password' BACKUP MASTER KEY TO FILE...
View ArticleHow To Change The Encryption Password On A Database Master Key
TODO:Have you ever wanted to change the password associated with your Database Master Key? SOLUTION:use my database GO --step 1, open the key OPEN MASTER KEY DECRYPTION BY PASSWORD = 'my password' GO...
View ArticleHow To Delete The Last X Records From A Table By Group
TODO:Have you ever wanted to keep the last X records for a Customer, Employee, Order, etc? SOLUTION:DELETE c FROM ( SELECT *, rn=row_number() OVER (PARTITION BY CustomerId ORDER BY Id desc) FROM...
View ArticleHow To Create IsWeekday Function In T-SQL To Determine If A Date Is A Weekday...
TODO:Have you ever wanted a function that tells you if a certain date is a Weekday? SOLUTION:CREATE FUNCTION IsWeekday(@InputDate datetime) RETURNS int AS BEGIN DECLARE @isweekday int select...
View Article
More Pages to Explore .....