T-SQL
IF EXISTS(SELECT * FROM sys.databases WHERE name = 'DBName')BEGIN
PRINT 'Database Exists.'
END
ELSE
BEGIN
PRINT 'Database does not exist.'
END
That’s It.
Enjoy Learning.
Practical lessons in Sitecore, AI, architecture and modern engineering.
T-SQL IF EXISTS( SELECT * FROM sys.databases WHERE name = 'DBName' ) BEGIN PRINT 'Database Exists.' END ELS...
T-SQL
IF EXISTS(SELECT * FROM sys.databases WHERE name = 'DBName')That’s It.
Enjoy Learning.
Created in last 15 Days SELECT ROUTINE_NAME AS 'Stored Procedure' , ROUTINE_DEFINITION AS 'Stored Procedure Text FROM INFORM...
Created in last 15 Days
SELECT ROUTINE_NAMEAS 'Stored Procedure'
,ROUTINE_DEFINITION AS 'Stored Procedure Text
FROM INFORMATION_SCHEMA.ROUTINES
WHERE
CREATED >= GETDATE()-15
AND
ROUTINE_TYPE='PROCEDURE'
ORDER BY ROUTINE_NAMEASC
Modified in last 15 Days
SELECT ROUTINE_NAMEAS 'Stored Procedure'
,ROUTINE_DEFINITION AS 'Stored Procedure Text
FROM INFORMATION_SCHEMA.ROUTINES
WHERE
LAST_ALTERED >= GETDATE()-30
AND
ROUTINE_TYPE='PROCEDURE'
ORDER BY ROUTINE_NAMEASC
That’s It.
Enjoy Learning.
We can use OPENROWSET for this case if we have credential details of remote SQL Server. T-SQL SELECT * FROM OPENROWSET ( 'SQLNCLI...
That’s It.
Enjoy Learning.
We have following select result records. And my desired output should be: T-SQL DECLARE @Table Table ( SQLForum VARCHAR ( 100 )) INSERT...
We have following select result records.
And my desired output should be:
T-SQL
DECLARE @Table Table
(SQLForum VARCHAR(100))
INSERT INTO @Table
SELECT 'MSDN' UNION ALL
SELECT 'StackOverFlow' UNION ALL
SELECT 'SQLServerCentral' UNION ALL
SELECT 'SQLTeam'
SELECT ROW_NUMBER()OVER(ORDER BY(SELECT 1))AS 'SRNo',* FROM @Table
That’s It.
Enjoy Learning.
I have following records in my table. And expected output should be: T-SQL DECLARE @Table Table ( Name VARCHAR ( 100 )) INSERT INTO @...
That’s It.
Enjoy Learning.
We have following records in our table. Now we need to fetch previous month data only. Means output should be: T-SQL DECLARE @TablePM TAB...
That’s It.
Enjoy Learning.
Sometimes it is very necessary to disable cut,copy & paste in a textbox. We can easily do it in ASP.Net. < asp : TextBox ID = "...
<asp:TextBox ID="txtDisableCutCopyPaste" runat="server" oncopy="return false"onpaste="return false" oncut="return false"></asp:TextBox>
It’s very handy technique.
That’s It.
Enjoy Learning.
We can see all types of syntax in Template Explorer. View ==> Template Explorer That’s It Enjoy Learning.
That’s It
Enjoy Learning.
We can replace paragraph mark with new line in MS-Word. Sometimes it makes my writing very easy for some forum. Replace ^p with ^l. That’s ...
That’s It
Enjoy Learning.
Using above statement we can find SQL server version.If it is 32 bit it will bring edition name only otherwise it will 64 bit added with ed...
Using above statement we can find SQL server version.If it is 32 bit it will bring edition
name only otherwise it will 64 bit added with edition name in output.
That’s It.
Enjoy Learning.
We can use CASE statement with UPDATE command in SQL Server. We have following records in table. Let’s try to update tables with case state...
We can use CASE statement with UPDATE command in SQL Server.
We have following records in table.
Let’s try to update tables with case statement.
T-SQL:
DECLARE @TableUpdateCase TABLE(Department VARCHAR(30),SalaryPercent INT)INSERT INTO @TableUpdateCaseSELECT 'IT',60 UNION ALLSELECT 'HR',20 UNION ALLSELECT 'TECH',40 UNION ALLSELECT 'ACCOUNT',30 UNION ALLSELECT 'ADMIN',40 UNION ALLSELECT 'SALES',80UPDATE @TableUpdateCaseSETSalaryPercent = SalaryPercent +CASEWHEN Department = 'IT' THEN 5WHEN Department = 'HR' THEN 4WHEN Department = 'TECH' THEN 9WHEN Department = 'ACCOUNT' THEN 7WHEN Department = 'ADMIN' THEN 9WHEN Department = 'SALES' THEN 8ENDSELECT * FROM @TableUpdateCase
After UPDATE table status has become:
That’s It.
Enjoy Learning.
We have bcp utility in SQL Server we can easily write query results to notepad file. T-SQL EXEC xp_cmdshell ' bcp "SELECT * FROM ...
We have bcp utility in SQL Server we can easily write query results to notepad file.
T-SQL
EXEC xp_cmdshell 'bcp "SELECT * FROM sys.objects"queryout "C:\ExportDataToNotepad.txt" -T -c -t,'
That’s It.
Enjoy Learning.
We have following records in our table. We want output should come in this format. This is best example of hierarchical query. Let’s try ...
We have following records in our table.
We want output should come in this format.
This is best example of hierarchical query.
Let’s try to write it.
T-SQL
CREATE TABLE TableCTE (ID int, ParentID int NULL, Name varchar(128));INSERT INTO TableCTE VALUES (1, NULL, 'A');INSERT INTO TableCTE VALUES (2, 1, 'B');INSERT INTO TableCTE VALUES (3, 2, 'C');INSERT INTO TableCTE VALUES (4, 3, 'D');INSERT INTO TableCTE VALUES (5, 4, 'E');SELECT * FROM TableCTEWITH Parent AS(SELECTID,ParentID,Name,Name AS PathFROMTableCTEWHEREParentID IS NULLUNION ALLSELECTTH.ID,TH.ParentID,TH.Name,CONVERT(varchar(128), Parent.Path + ',' + TH.Name) AS PathFROMTableCTE THINNER JOINParentONParent.ID = TH.ParentID)SELECT * FROM Parent
That’s It.
Enjoy Learning.
How can we insert stored procedure result in a temporary table. CREATE PROC spGetDateList AS BEGIN SELECT GETDATE() AS [ DATE ] U...
CREATE PROC spGetDateListASBEGINSELECT GETDATE() AS [DATE] UNION ALLSELECT GETDATE()+1 AS [DATE] UNION ALLSELECT GETDATE()+2 AS [DATE] UNION ALLSELECT GETDATE()+3 AS [DATE] UNION ALLSELECT GETDATE()+4ENDGOsp_configure 'Show Advanced Options', 1GORECONFIGUREGOsp_configure 'Ad Hoc Distributed Queries', 1GORECONFIGUREGOSELECT * INTO #TempTable FROMOPENROWSET('SQLNCLI', 'Server=ServerInstanceName;Trusted_Connection=yes;','EXEC Database.Schema.StoredProcedure')
How can we find cumulative sum in SQL Server. T-SQL DECLARE @Table TABLE (ID INT ,Amount INT ) INSERT INTO @Table SELECT 1, 200 UNION A...
That’s It
Enjoy Learning.
Here is a function that will remove all characters from string. CREATE FUNCTION [dbo].[RemoveCharacters](@Str VARCHAR (500)) RETURNS VARC...
CREATE FUNCTION [dbo].[RemoveCharacters](@Str VARCHAR(500))RETURNS VARCHAR(500)ASBEGINWHILE PatIndex('%[^0-9]%', @Str) > 0SET @Str = Stuff(@Str, PatIndex('%[^0-9]%', @Str), 1, '')RETURN @StrENDDECLARE @Str VARCHAR(100)SET @Str = 'A1X2Y3'SELECT [dbo].RemoveCharacters(@Str)
Select current entries from table. T-SQL DECLARE @TableX TABLE ([ Date ] DATETIME ) INSERT INTO @TableX SELECT ' 2011-11-28 ' UN...
DECLARE @TableX TABLE([Date] DATETIME)INSERT INTO @TableXSELECT '2011-11-28' UNION ALLSELECT '2011-11-25' UNION ALLSELECT '2011-11-27' UNION ALLSELECT '2011-11-29' UNION ALLSELECT '2011-11-30' UNION ALLSELECT '2011-11-11' UNION ALLSELECT '2011-11-21' UNION ALLSELECT '2011-12-01'SELECT * FROM @TableXWHEREDATE BETWEENDATEADD(DD, 1 - DATEPART(DW, GETDATE()),GETDATE())ANDDATEADD(DD, 1 - DATEPART(DW, GETDATE()),GETDATE())+7
That’s It.
Enjoy Learning.
© Shatrughna Kumar. The Tech Pursuit.
Views expressed here are personal and do not represent any employer.