SQL Skip to main content

SQL

I am going to share some basic and advance SQL Query that is generally used by you in day to day development retune so please see following important sql query-
1.
--To know about total tables count
SELECT COUNT(*) AS TablesCount FROM sys.tables
2.
--To know about total procedures count
SELECT COUNT(*) AS TotalCount FROM sys.procedures
3.
--To know about total views in same db
SELECT COUNT(*) AS TotalCount FROM sys.views
4.
--To Know result of following query
SELECT CASE WHEN NULL = NULL THEN 'Yup' ELSE 'Nope' END AS Result;
5.
--To check null column if null then pass defual value
SELECT ISNULL(columnName,'NA') FROM tableName
6.
--RANK() v/s DENSE_RANK()
SELECT RANK() OVER (ORDER BY columnName) AS ROW_RANK FROM  tableName
-- return rank with gap if same record e.g. 22,22,34,45 then return row value i.e.- 1,1,3,4
7.
SELECT DENSE_RANK() OVER (ORDER BY columnName) AS ROW_DENSE_RANK FROM tableName
-- return rank with any gap if same record e.g. 22,22,34,45 then return row value i.e.- 1,1,2,4
8.
--To get top ten records form starting
SELECT DISTINCT TOP 10 columnName FROM tableName
ORDER BY columnName DESC
9.
--To get only % from a column in the table
SELECT * FROM tableName WHERE columnName like '%[%]%'
10.
--To know current datetime of sql server system
Select CURRENT_TIMESTAMP
SELECT GETDATE()
SELECT SYSDATETIME()
11.
--Return dirty data, including updated in isolation transaction data
SELECT * FROM tableName WITH(NOLOCK)
12.
--Return data that are not locked by any trasaction
SELECT * FROM tableName WITH(READPAST)
13.
--If set following query in the starting of procedure then we get dirty data from all the tables
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
14.
--If set following query in the starting of procedure then we get commited data from all the tables
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
15.
--Insert records into table using direct proc
GO
CREATE PROC procInsertRecords
AS
SET NOCOUNT ON
SELECT 'A' AS Col1,123 AS Col2

CREATE TABLE #TEMP (
DATABASE_NAME VARCHAR(128),
DATABASE_SIZE INT);

--Insert the output of stored procedures into temp table
INSERT INTO #TEMP EXEC procInsertRecords;

--Return Data From temp table
SELECT * FROM #TEMP;

-- CLEAN UP
DROP TABLE #TEMP;
--End insert records into table using direct proc
16.
--To stop t-sql running query for five seconds
 WAITFOR DELAY '00:00:05'
17.
--This option specifies the setting for ANSI NULL comparisons. When this is on then if any query that compares a value with a null returns a 0 record. When off, any query that compares a value with a null returns a null value.
SET ANSI_NULLS OFF
SELECT * FROM tableName WHERE columnName = NULL
18.
--This options specifies the setting for usage of double quotation. When this is on then double quotation mark is used as part of the SQL Server identifier (object name). This can be useful in situations in which identifiers are also SQL Server reserved words.
 SET QUOTED_IDENTIFIER ON
 CREATE TABLE "tableName"(Id INT,Name VARCHAR(10))
 --above table successfully created if QUOTED_IDENTIFIER is on and in case of off then getting error.
19.
--If it ON then No value return in message of sql outbox
SET NOCOUNT OFF
INSERT INTO tableName VALUES ('BizTalkLive',NULL)
20.
--When used it in starting of procedure as SET XACT_ABORT is ON, if a Transact-SQL statement raises a run-time error, the entire transaction is terminated and rolled back. When SET XACT_ABORT is OFF, in some cases only the Transact-SQL statement that raised the error is rolled back and the transaction continues processing. Depending upon the severity of the error, the entire transaction may be rolled back even when SET XACT_ABORT is OFF. OFF is the default setting.
SET XACT_ABORT ON
21.
--GET all data of last 5th all rows from table
SELECT TOP 1 * FROM tableName
WHERE columnId IN (SELECT TOP 5 columnId FROM tableName ORDER BY columnId DESC)
22.
--Difference between charindex and patindex:  Patindex - working with only Wildcard "%" to get integer value of first match case while in charindex we can no include wildcard "%" only "'" and resutl is same in both case.
SELECT PATINDEX('%ter%', 'interesting data'); 
SELECT CHARINDEX('ter', 'interesting data');
23.
--STUFF function
SELECT STUFF('BizTalkJitendra', 8, 8, 'Live')
--Result: BizTalkLive :: explanation: insert second string value Live at 8th position and delete 8 values from first string i.e. J

!! Keep Visiting BizTalkLive !!

Comments

Popular posts from this blog

BizTalk Interview Question and Answer

1. What is BizTalk? BizTalk is a middleware that sits in the middle of any two software who wish to communicate with each other and agree on some specified communication pattern. It uses SQL Server as back end database. “Microsoft BizTalk Server is an Inter-Organizational Middleware System (IOMS) that enables companies to automate business processes, through the use of adapters which are tailored to communicate with different software systems used in an enterprise. Created by Microsoft, it provides enterprise application integration, business process automation, business-to-business communication, message broker and business activity monitoring.”                 2. What is BizTalk Server Architecture and Life Cycle of Message? BizTalk Server Architecture: Life Cycle of Message: In this simplified view, a message is received through a receive location defined...

Configuring BizTalk WCF Timeout Values on a Binding

There are a number of timeout settings available in WCF bindings. Setting these timeout settings correctly can improve not only your service’s performance but also play a role in the usability and security of your service. The following timeouts are available on WCF bindings: OpenTimeout CloseTimeout SendTimeout ReceiveTimeout Open Timeout :  This property represents the amount of time a channel open operation has to complete. Send Timeout :  Use this property to set the amount of time that a send operation has to complete. When used as part of a solicit-response scenario, this value encompasses the total amount of time for the interaction to complete. If we are sending a large message, we may need to increase this timeout to allow for the request and response messages to be processed within this window. Close Timeout :  A time-stamp that is used to indicate the amount Receive Timeout :  Used by the Service Framework Layer to initialize the...

PowerShell Script to enable BizTalk Receive Location

We are facing problem in BizTalk server 2016 that few of running SFTP Receive location would be stopped after some time due to sftp connectivity issue of their threshold so I am going to share PowerShell script to enable receive location by window task scheduler. Before execute following script Please do following task a. Set Server name in ConnectionString b. Set ( $hostname) host name value that is using in SFTP Receive location c. Set ( $rcvLocation) Receive location name # Import external assembly and create a new object [ void ] [ System.reflection.Assembly ]:: LoadWithPartialName( "Microsoft.BizTalk.ExplorerOM" ) $Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer   #BizTalk Config $Catalog . ConnectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI"   #connectionstring for the BizTalkMgmtDb $hostname = "BizTalkServerApplication" #hostname $rcvLocation = "RL_GetAccReq_SQL...

Publish a web service in biztalk using BizTalk WCF Service Publishing Wizard

Welcome you again in my Blog and let us do another mind blowing achievement that how to publish a WCF web service in BizTalk and as in previous post to Complete BizTalk project to host Schema as Web Service  so If you want to expose/publish a web service, you should run through the BizTalk WCF Service Publishing Wizard. This wizard guides you through choosing what you want to expose as a web service. This section walks you through the wizard step by step to publish WCF Web Service using  WCF-WSHttp- Step-1:  To start the wizard, go to Start, All Programs, Microsoft BizTalk Server 2016(version), BizTalk WCF Service Publishing Wizard. This fires up a welcoming screen, on which you just click Next. Consider disabling the welcoming screen for the future by checking the check box at the bottom. You will now see the screen shown in above figure. Step-2: Choose the Service Endpoint option, and choose which adapter to use. Check Enable Metadata Endpoint if you wa...

Step by step solution of BizTalk Two-Way WCF-SQL XmlPolling in Send Port

I am going to share most awaited Two way WCF_SQL Two Way XmlPolling so that you can easily enhance your old SQL procedure and BizTalk Map without any big change. I know there is no content available in any website to how work with XmlPolling in WCF-SQL two-way adapter. In the last of this post you can download complete source code with an example. Please follow carefully all steps – 1. Open Microsoft Visual Studio as Administrator user and Click File>New>Project… In Installed Templates click BizTalk Projects then Empty BizTalk Server Project and Name as “BizTalkLive-WCF-SQL” and click OK. 2. Now see the below picture and create all respective folders separately for Schema, Map, Orchestration, Pipeline, Binding- 3. Create Schemas in Schema folder as shown below picture to get Account request using One Way WCF-SQL XmlPolling a) Create Envelope Schema first with Target NameSpace - http://BizTalkLive_WCF_SQL.AccountRequest b) Create Second Sch...