State your problem
Save the work
Break down
Restructure
Check cardinalities
Check filter constraints
Validate changes
Perform refactoring
Document
A Software Engineer and data professional's blog on SQL, data, databases, data architectures, data management, programming, Software Engineering, Project Management, ERP implementation and other IT related topics.
"A SQL Server role created to support the security needs of an application. Such a role is activated by a password and the use of the spsetapprole system stored procedure." (Microsoft Corporation, "SQL Server 7.0 System Administration Training Kit", 1999)
"A SQL Server role created to support the security needs of an application. Using application roles is an alternative to allowing users access to SQL Server 2000. You can create an application role and assign it to a particular application, allowing users who use the application to access SQL Server." (Anthony Sequeira & Brian Alderman, "The SQL Server 2000 Book", 2003)
"This is a special type of role that requires activation using the sp_setapprole stored procedure. This is primarily used to keep users from accessing a database with anything other than a custom application." (Joseph L Jorden & Dandy Weyn, "MCTS Microsoft SQL Server 2005: Implementation and Maintenance Study Guide - Exam 70-431", 2006)
"A SQL Server role used by the application, instead of the user, to authenticate against a database solution." (Marilyn Miller-White et al, "MCITP Administrator: Microsoft® SQL Server™ 2005 Optimization and Maintenance 70-444", 2007)
"A SQL Server role used by the application, instead of the user, to authenticate against a database solution." (Victor Isakov et al, "MCITP Administrator: Microsoft SQL Server 2005 Optimization and Maintenance (70-444) Study Guide", 2007)
"A SQL Server role created to support the security needs of an application." (Microsoft, "SQL Server 2012 Glossary", 2012)
-- examples with replace DECLARE @str varchar(30) SET @str = 'this is a test string' SELECT replace(@str, ' ', ',') Example1 , replace(@str, ' ', ' ') Example2 , replace(@str, ' ', '') Example3 , replace(@str, 'is', 'as') Example4
Example1 | Example2 | Example3 | Example4 |
this,is,a,test,string | this is a test string | thisisateststring | thas as a test string |
-- replacement with check DECLARE @str varchar(30) DECLARE @search varchar(30) DECLARE @replacememt varchar(30) SET @str = 'this is a test string' SET @search = 'this string' SET @replacememt = 'other string' SELECT CASE WHEN CharIndex(@search, @str)>0 THEN Replace(@str, @search, @replacememt) ELSE @str END result
-- replacement starting with a given position DECLARE @str varchar(30) DECLARE @search varchar(30) DECLARE @replacememt varchar(30) DECLARE @start int SET @str = 'this is a test string' SET @search = 's' SET @replacememt = 'x' SET @start = 7 SELECT Left(@str, @start-1) FirstPart , RIGHT(@str, Len(@str)-@start+1) SecondPart , CASE WHEN @start <= LEN(@str) THEN Left(@str, @start-1) + Replace(RIGHT(@str, Len(@str)-@start+1), @search, @replacememt) ELSE @str END Replacement
FirstPart | SecondPart | Replacement |
this i | s a test string | this ix a text xtring |
-- Stuff-based examplesDECLARE @str varchar(30)SET @str = 'this is a test string' SELECT STUFF(@str, 6, 2, 'was ') Example1 , STUFF(@str, 1, 0, 'and ') Example2 , STUFF(@str, 1, 0, 'that') Example3 , STUFF(@str, LEN(@str) + 1, 0, '!') Example4
Example1 | Example2 | Example3 | Example4 |
this was a test string | and this is a test string | thatthis is a test string | NULL |
-- left/right padding
DECLARE @str varchar(30) DECLARE @length int DECLARE @padchar varchar(1) SET @str = '12345' SET @length = 10 SET @padchar = '0' SELECT @str StringToPad , CASE WHEN LEN(@str)<@length THEN Replicate(@padchar, @length-LEN(@str)) + @str ELSE @str END LeftPadding , CASE WHEN LEN(@str)<@length THEN @str + Replicate(@padchar, @length-LEN(@str)) ELSE @str END RightPadding
StringToPad | LeftPadding | RightPadding |
12345 | 0000012345 | 1234500000 |
-- parsing a VAT Number DECLARE @VAT varchar(50) SET @VAT = 'CH9300762011623852957' SELECT @VAT VATNumber , LEFT(@VAT, 2) CountryCode1 , SUBSTRING(@VAT, 1, 2) CountryCode2 , SUBSTRING(@VAT, 3, 2) CheckDigits , RIGHT(@VAT, Len(@VAT)-4) BBAN1 , SUBSTRING(@VAT, 5, Len(@VAT)-4) BBAN2 , CASE WHEN LEN(@str)<@length THEN Replicate(@padchar, @length-LEN(@str)) + @str ELSE @str END LeftPadding , CASE WHEN LEN(@str)<@length THEN @str + Replicate(@padchar, @length-LEN(@str)) ELSE @str END RightPadding
-- parsing a Person's Name DECLARE @Name varchar(50) SET @Name = 'Stone Sharon' SELECT @Name Name , LEFT(@Name, CHARINDEX(' ', @Name)) LastName , RIGHT(@Name, LEN(@Name) - CHARINDEX(' ', @Name)) FirstName
Name | LastName | FirstName |
Stone Sharon | Stone | Sharon |
-- extracting the words from a sentence DECLARE @str nvarchar(100) SET @str = 'This is just a test' ;WITH CTE (PrevString, Position, Word) AS ( SELECT LTrim(RTrim( CASE WHEN CharIndex(' ', @str)>0 THEN Right(@str, Len(@str)-CharIndex(' ', @str)) ELSE '' END)) PrevString , 1 Position , LTrim(RTrim(CASE WHEN CharIndex(' ', @str)>0 THEN LEFT(@str, CharIndex(' ', @str)) ELSE @str END)) Word UNION ALL SELECT LTrim(RTrim(CASE WHEN CharIndex(' ', PrevString)>0 THEN Right(PrevString, Len(PrevString)-CharIndex(' ', PrevString)) ELSE '' END)) PrevString , Position + 1 Position , LTrim(RTrim(CASE WHEN CharIndex(' ', PrevString)>0 THEN LEFT(PrevString, CharIndex(' ', PrevString)) ELSE PrevString END)) Word FROM CTE WHERE Len(PrevString)>0 ) SELECT PrevString , Word , Position FROM CTE OPTION (maxrecursion 100)
PrevString | Word | Position |
is just a test | This | 1 |
just a test | is | 2 |
a test | just | 3 |
test | a | 4 |
test | 5 |
-- dropping the table -- DROP TABLE dbo.TestTable --declaring the table CREATE TABLE dbo.TestTable ( id int primary key , value varchar (50)) -- inserting the records in a loop DECLARE @index int SET @index=1 SET NOCOUNT ON WHILE (@index<100000) BEGIN INSERT INTO dbo.TestTable VALUES (@index,'test '+cast(@index as varchar(10))) SET @index=@index+1 END
DECLARE @max as int
-- deletion based on stored max value DECLARE @max int SELECT @max = MAX(ID) FROM( SELECT top(10000) id FROM TestTable ORDER BY ID ) A --deleting the data DELETE FROM TestTable WHERE id <= @max
-- deletion based on max value - within one query DELETE FROM TestTable WHERE id <= ( SELECT MAX(ID) FROM ( SELECT top(10000) id FROM dbo.TestTable ORDER BY ID ) A ) --deleting the data DELETE FROM TestTable WHERE id <= @max
-- deleting the data within a given interval DELETE FROM dbo.TestTable WHERE id BETWEEN @start AND @end
-- creating the UDF function CREATE FUNCTION dbo.TestUDF1( @start int , @end int) RETURNS TABLE AS RETURN ( SELECT id, value FROM dbo.TestTable WHERE id BETWEEN @start AND @end ) --deleting the data DELETE FROM dbo.TestUDF1(20001, 30000)
-- creating the view CREATE VIEW dbo.vTestView AS SELECT top(10000) id FROM dbo.TestTable ORDER BY ID --deleting the data DELETE FROM dbo.vTestView
-- creating the UDF function CREATE FUNCTION dbo.TestUDF2( @number_records int) RETURNS TABLE AS RETURN ( SELECT top (@number_records) id, value FROM dbo.TestTable ORDER BY id ) --deleting the data DELETE FROM dbo.TestUDF2(10001)
-- deletion using a CTE ;WITH CTE AS ( SELECT top(10000) id FROM dbo.TestTable ORDER BY ID ) DELETE FROM CTE
-- review record count SELECT count(*) FROM dbo.TestTable;
-- trimming a string SELECT LTrim(' this is a string ') Length1 -- left trimming , RTrim(' this is a string ') Length2 --right trimming , LTrim(RTrim(' this is a string ')) Length2 --left & right trimming
-- length of a string SELECT Len('this is a string') Length1 -- simple string , Len('this is a string ') Length2 --ending in space , Len(' this is a string') Length3 --starting with a space , Len(' this is a string ') Length4 --starting & ending with a space , Len(LTrim(' this is a string ')) Length5 --length & left trimming
,Len(RTrim(' this is a string ')) Length5 --length & right trimming
,Len(LTrim(RTrim(' this is a string '))) Length5 --length, left & right trimming
-- sample comparisons SELECT CASE WHEN 'abc' != 'abc ' THEN 1 ELSE 0 END Example1 , CASE WHEN ' abc' != 'abc' THEN 1 ELSE 0 END Example2 , CASE WHEN ' ' != '' THEN 1 ELSE 0 END Example3 -- error comparison , CASE WHEN 'abc' != NULL THEN 1 ELSE 0 END Example4 , CASE WHEN 'abc' = NULL THEN 1 ELSE 0 END Example5 -- adequate NULL comparison , CASE WHEN 'abc' IS NOT NULL THEN 1 ELSE 0 END Example6 , CASE WHEN 'abc' IS NULL THEN 1 ELSE 0 END Example7
Example1 | Example2 | Example3 | Example5 | Example7 |
0 | 1 | 0 | 0 | 0 |
-- sample comparisons (case sensitive) SELECT CASE WHEN 'abc' < 'ABC' THEN 1 ELSE 0 END Example1 , CASE WHEN 'abc' > 'abc' THEN 1 ELSE 0 END Example2 , CASE WHEN 'abc' >= 'abc ' THEN 1 ELSE 0 END Example3 , CASE WHEN 'abc' <> 'ABC' THEN 1 ELSE 0 END Example4 , CASE WHEN 'abc' > '' THEN 1 ELSE 0 END Example5 , CASE WHEN ' ' > '' THEN 1 ELSE 0 END Example6
Example1 | Example2 | Example3 | Example4 | Example5 | Example6 |
0 | 0 | 1 | 0 | 1 | 0 |
--enabling case sensitivity for a database ALTER DATABASE <database name> COLLATE Latin1_General_CS_AS --disabling case sensitivity for a database ALTER DATABASE <database name> COLLATE Latin1_General_CI_AS
Example1 | Example2 | Example3 | Example4 | Example5 | Example6 |
1 | 0 | 1 | 1 | 1 | 0 |