| 1 | ~ (Bitwise NOT) |
| 2 | * (Multiply), / (Division), % (Modulo) |
| 3 | + (Positive), - (Negative), + (Add), (+ Concatenate), - (Subtract), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR) |
| 4 | =, >, <, >=, <=, <>, !=, !>, !< (Comparison operators) |
| 5 | NOT |
| 6 | AND |
| 7 | ALL, ANY, BETWEEN, IN, LIKE, OR, SOME |
| 8 | = (Assignment) |
Sunday, April 17, 2011
SQL server order of precedence
Monday, December 14, 2009
Data Types in SQL Server 2008
, i am sharing some basic info.
Strings
Here we will discuss Char, VarChar, and Text. As most of you might me aware of difference between char and varchar, where when a char(50) and varchar(50) columns are defined, char takes up 50 Bytes in respective of size of content saved in the column where varchar is flexible and takes up bytes required to save the length of the content.
Char must be used in case of where it is know or understood that length of the text would be fixed like a char(1) column would make much more sense in case of a Sex column where only M/F is expected to saved instead of varchar, where as something like name should be varchar.
Varchar has a max limit of 8000, though a new introduction of Varchar(max) has made it more flexible as the size can be unlimited in this case, and the main benefit of this is that as in case of Text column string operations cannot be done, while here all the string operations can be handled easily. Also the thing to note here is that Varchar(max) is all set to replace text in the future versions of SQL server as Text is supported in 2008 only as a part of backward compatibility.
I will adding more to this, hope you like it.... please Digg, Kick or share this if you like this.
Thursday, October 29, 2009
SQL Server Database analysis scripts
While browsing the net my team came across couple of very good analytical scripts, i would like to share this with everyone. often there is a scenario where you wonder how to find out what script or procedure is consuming most memory or have the most reads, this help to identify the possible bottle necks in the website.
SELECT TOP 10 qt.TEXT AS 'SP Name',Though this is not as useful as query one, but can be handy in case of really large databases, where you need to identify what table is consuming how much memory, and may be the the data in some tables is not all that relevant now and can be deleted conditionally, help improve the performance.
qs.execution_count AS 'Execution Count',
qs.execution_count/DATEDIFF(Second, qs.creation_time, GETDATE()) AS 'Calls/Second',
qs.total_worker_time/qs.execution_count AS 'AvgWorkerTime',
qs.total_worker_time AS 'TotalWorkerTime',
qs.total_physical_reads AS 'PhysicalReads',
qs.creation_time 'CreationTime'
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt
WHERE qt.dbid = (
SELECT dbid
FROM sys.sysdatabases
WHERE name = 'DatabaseName')
ORDER BY qs.total_physical_reads DESC
2) Query to check the size of each table:
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
One major point, you need admin/sa access to run these scripts
Look forward to your comments on the same
Tuesday, July 21, 2009
Performance optimizing the SQL Store Procedure
NO COUNT
By default, every time a stored procedure is executed, a message is sent from the server to the client indicating the number of rows that were affected by the stored procedure. Rarely is this information useful to the client. By turning off this default behavior, you can reduce network traffic between the server and the client, helping to boost overall performance of your server and applications.
To turn this feature off on at the stored procedure level, you can include the statement:
SET NOCOUNT ON at the beginning of each stored procedure you write. This statement should be included in every stored procedure you write.
If you want this feature turned off for your entire server, you can do this by running these statements at your server:
SP_CONFIGURE 'user options', 512 RECONFIGURE
but i don't recommend this as in certain cases you might need that all important row count.
Keep Transact-SQL transactions as short as possible within a stored procedure. This helps to reduce the number of locks, helping to speed up the overall performance of your SQL Server application.
Three ways to help reduce the length of a transaction are to:
1) break up the entire job into smaller steps (or multiple stored procedures or user defined functions) so each step can be committed as soon as possible
2) take advantage of SQL Server statement batches, which acts to reduce the number of round-trips between the client and server.
3) if there is a certain set of statement or calculations that is being done repeatedly in procedures use user defined functions to reduce code size and improve performance
Procedure Naming
If you are creating a stored procedure to run in a database other than the Master database, don't use the prefix "sp_" in its name. This special prefix is reserved for system stored procedures. Although using this prefix will not prevent a user defined stored procedure from working, what it can do is to slow down its execution ever so slightly.
The reason for this is that by default, any stored procedure executed by SQL Server that begins with the prefix "sp_", is first attempted to be resolved in the Master database. Since it is not there, time is wasted looking for the stored procedure.
If SQL Server cannot find the stored procedure in the Master database, then it next tries to resolve the stored procedure name as if the owner of the object is "dbo". Assuming the stored procedure is in the current database, it will then execute. To avoid this unnecessary delay, don't name any of your stored procedures with the prefix "sp_"
Before you are done with your stored procedure code, review it for any unused code, parameters, or variables that you may have forgotten to remove while you were making changes, and remove them. Unused code just adds unnecessary bloat to your stored procedures, although it will not necessarily negatively affect performance of the stored procedure.
Executing Strings
When you need to execute a string of Transact-SQL, you should use the sp_executesql stored procedure instead of the EXECUTE statement. Sp_executesql offers two major advantages over EXECUTE. First, it supports parameter substitution, which gives your more options when creating your code. Second, it creates query execution plans that are more likely to be reused by SQL Server, which in turn reduces overhead on the server, boosting performance.
Sp_executesql executes a string of Transact-SQL in its own self-contained batch. When it is run, SQL Server compiles the code in the string into an execution plan that is separate from the batch that contained the sp_executesql and its string.
Thursday, July 2, 2009
Benefits of Stored Procedures - Explained (Part 1)
In case you have any questions or comments please post them i will try and respond.
Monday, June 29, 2009
Benefits of Stored Procedures
- Reduced network traffic and latency, boosting application performance.
- Stored procedure execution plans can be reused, staying cached in SQL Server's memory, reducing server overhead.
- Client execution requests are more efficient. For example, if an application needs to INSERT a large binary value into an image data column not using a stored procedure, it must convert the binary value to a character string (which doubles its size), and send it to SQL Server. When SQL Server receives it, it then must convert the character value back to the binary format. This is a lot of wasted overhead. A stored procedure eliminates this issue as parameter values stay in the binary format all the way from the application to SQL Server, reducing overhead and boosting performance.
- Stored procedures help promote code reuse. While this does not directly boost an application's performance, it can boost the productivity of developers by reducing the amount of code required, along with reducing debugging time.
- Stored procedures can encapsulate logic. You can change stored procedure code without affecting clients (assuming you keep the parameters the same and don't remove any result sets columns). This saves developer time.
- Stored procedures provide better security to your data. If you use stored procedures exclusively, you can remove direct SELECT, INSERT, UPDATE, and DELETE rights from the tables and force developers to use stored procedures as the method for data access.
Wednesday, June 24, 2009
Use view to improve query performance
Recently I came across two such scenarios where a large dataset (1,000,000 + rows) was being retrieved using a complex set of joins in stored procedure, we did all the regular stuff
1. Checked Indexes - made sure they are on the right columns and verified the fill factor
2. Re indexed the database for better performance
3. Checked and removed the non essential joins
4. Verified join types
we did have some improvement in performance, but nothing like we had with using views instead of complex select query, we were able to get performance levels increased by 10 t0 40 times in specific cases.
Now the question is why?
I read the following while searching on another topic and it struck me, a view is an optimized query object, SQL server itself chooses the best execution plan and then when we do a filter on the view the nested loops are decreased considerably as only one select is being filtered rather that all the tables in the join..... It really helps
From the Database Management System (DBMS) perspective, a view is a description of the data (a form of metadata). When a typical view is created, the metadata is defined by encapsulating a SELECT statement that defines a result set to be represented as a virtual table. When a view is referenced in the FROM clause of another query, this metadata is retrieved from the system catalog and expanded in place of the view's reference. After view expansion, the SQL Server query optimizer compiles a single execution plan for the executing query. The query optimizer searches though a set of possible execution plans for a query, and chooses the lowest-cost plan it can find, based on estimates of the actual time it will take to execute each query plan.
Thursday, April 30, 2009
UPDATE script with Joins
I know this is basic but we tend to forget the basic stuff, today when a client asked me to update the date from one table into another table with 100's of records i for a moment thought that’s manual data entry and then it struck me a simple update statement will do it. Guess this is bound to happen if you work more at client end and not development.
well here is a sample of what i did and i was amazed that if i had opted for manual data entry it would have taken me hours. and the script took me 5~6 minutes.
here is what i did
update Table1 set Table1.col1= Table2.col2 from Table2 where Table1.Id= Table2.id
of course I could have used Inner join or any other standard join as and when required.
Thursday, November 20, 2008
Insert Script for MS SQL Server
/***** Object: StoredProcedure [dbo].[sp_CreateDataLoadScript] Script
******/SET ANSI_NULLS ONGOSETQUOTED_IDENTIFIER ON
GO
Create Procedure[dbo].[sp_CreateDataLoadScript]@TblName
varchar(128)as
/*execsp_CreateDataLoadScript 'MyTable'*/
create table #a (id intidentity (1,1), ColType int, ColName varchar(128))
insert #a
(ColType,ColName) select case when DATA_TYPE like '%char%' then 1 else 0 end
,COLUMN_NAME from information_schema.columns where TABLE_NAME =@TblNameorder by ORDINAL_POSITIONif not exists (select * from #a)
begin
raiserror('No columns found for table %s', 16,-1, @TblName)
return
enddeclare @id int ,@maxid int,@cmd1 varchar(7000) ,@cmd2 varchar(7000)
select @id = 0 ,@maxid =max(id)from
#aselect @cmd1 = 'select '' insert ' + @TblName + ' ('select @cmd2 = ' + ''
select '' + 'while @id < @maxid begin select@id = min(id) from #a where id > @idselect@cmd1 = @cmd1 + ColName +',' from #a where id = @idselect
@cmd2 = @cmd2+ ' casewhen ' + ColName + ' is null '+ ' then ''null'' '+ ' else '+
case when ColType = 1 then ''''''''' + ' + ColName + ' + '''''''''
else 'convert(varchar(20),' + ColName + ')' end+ ' end + '','' + 'from #a
where id = @idendselect@cmd1 = left(@cmd1,len(@cmd1)-1) +' ) '' '
select @cmd2 = left(@cmd2,len(@cmd2)-8) + ' from ' + @tblNameselect '
/*' + @cmd1 + @cmd2 +'*/'
exec (@cmd1 + @cmd2)
droptable #a
Note: you will have to turn of identity column if you want to insert the primary key as well.
Friday, November 14, 2008
SQL Server - Tips and more
1. Changing owner of tables/ procedure or Function.
For Tables
DECLARE @old sysname, @new sysname, @sql varchar(1000)
SELECT
@old =
'oldOwner_CHANGE_THIS'
, @new = 'dbo'
, @sql = '
IF EXISTS (SELECT
NULL FROM INFORMATION_SCHEMA.TABLES
WHERE
QUOTENAME(TABLE_SCHEMA)+''.''+QUOTENAME(TABLE_NAME) = ''?''
AND
TABLE_SCHEMA = ''' + @old + '''
)
EXECUTE sp_changeobjectowner ''?'',
''' + @new + ''''
EXECUTE sp_MSforeachtable @sql
For Procedures
DECLARE @oldOwner sysname, @newOwner sysname
SELECT
@oldOwner =
'oldOwner_CHANGE_THIS'
, @newOwner = 'dbo'
select 'EXECUTE
sp_changeobjectowner
'''+QUOTENAME(a.SPECIFIC_SCHEMA)+'.'+QUOTENAME(a.ROUTINE_NAME)+''','''+@newOwner+''''
from
INFORMATION_SCHEMA.ROUTINES a
where
a.ROUTINE_TYPE =
'PROCEDURE'
AND a.SPECIFIC_SCHEMA = @oldOwner
AND
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(a.SPECIFIC_SCHEMA)+'.'+QUOTENAME(a.ROUTINE_NAME)),
'IsMSShipped') = 0
For Functions
DECLARE @oldOwner sysname, @newOwner sysname
SELECT
@oldOwner =
'oldOwner_CHANGE_THIS'
, @newOwner = 'dbo'
select 'EXECUTE
sp_changeobjectowner
'''+QUOTENAME(a.SPECIFIC_SCHEMA)+'.'+QUOTENAME(a.ROUTINE_NAME)+''','''+@newOwner+''''
from
INFORMATION_SCHEMA.ROUTINES a
where
a.ROUTINE_TYPE =
'Function'
AND a.SPECIFIC_SCHEMA = @oldOwner
AND
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(a.SPECIFIC_SCHEMA)+'.'+QUOTENAME(a.ROUTINE_NAME)),
'IsMSShipped') = 0
2. Insert Script for SQL Server
Browsing the net i found this great script it creates a procedure with takes table name as input and returns a insert script for table.
****** Object: StoredProcedure [dbo].[sp_CreateDataLoadScript] Script Date:
09/19/2008 20:53:38 ******/
SET ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
Create Procedure
[dbo].[sp_CreateDataLoadScript]
@TblName varchar(128)
as
/*
exec
sp_CreateDataLoadScript 'MyTable'
*/
create table #a (id int
identity (1,1), ColType int, ColName varchar(128))
insert #a (ColType,
ColName)
select case when DATA_TYPE like '%char%' then 1 else 0 end ,
COLUMN_NAME
from information_schema.columns
where TABLE_NAME =
@TblName
order by ORDINAL_POSITION
if not exists (select * from #a)
begin
raiserror('No columns found for table %s', 16,-1, @TblName)
return
end
declare @id int ,
@maxid int ,
@cmd1
varchar(7000) ,
@cmd2 varchar(7000)
select @id = 0 ,
@maxid =
max(id)
from #a
select @cmd1 = 'select '' insert ' + @TblName + ' (
'
select @cmd2 = ' + '' select '' + '
while @id < @maxid begin select
@id = min(id) from #a where id > @id
select @cmd1 = @cmd1 + ColName +
','
from #a
where id = @id
select @cmd2 = @cmd2
+ ' case
when ' + ColName + ' is null '
+ ' then ''null'' '
+ ' else '
+ case
when ColType = 1 then ''''''''' + ' + ColName + ' + ''''''''' else
'convert(varchar(20),' + ColName + ')' end
+ ' end + '','' + '
from #a
where id = @id
end
select @cmd1 = left(@cmd1,len(@cmd1)-1) +
' ) '' '
select @cmd2 = left(@cmd2,len(@cmd2)-8) + ' from ' + @tblName
select '/*' + @cmd1 + @cmd2 + '*/'
exec (@cmd1 + @cmd2)
drop
table #a
Note: you will have to turn of identity column if you want to insert the primary key as well.
Tuesday, July 3, 2007
Retrieving Distinct string values without using Distinct in T-SQL
Recently I came across a scenario where I needed to get distinct string values sorted by date time without sorting it in alphabetical order. I was dumb struck to find none of the SQL Server methods provided direct support to do so. I finally came up with the below mentioned process to achieve this, though would not say that it’s the best way forward, but till I find a better way, I am sharing the best I know.
(
ID INT IDENTITY,
Value VARCHAR(50)
)
DECLARE @Table2 TABLE
(
ID INT IDENTITY,
Value VARCHAR(50)
)
INSERT INTO @Table1(Value)
Values('A')
INSERT INTO @Table1(Value)
Values('Z')
INSERT INTO @Table1(Value)
Values('B')
INSERT INTO @Table1(Value)
Values('F')
INSERT INTO @Table1(Value)
Values('A')
INSERT INTO @Table1(Value)
Values('F')
INSERT INTO @Table1(Value)
Values('N')
INSERT INTO @Table1(Value)
Values('C')
INSERT INTO @Table1(Value)
Values('N')
INSERT INTO @Table1(Value)
Values('B')
DECLARE @LOOP INT
DECLARE @COUNT INT
SET @LOOP =1
SELECT @COUNT = Count(*) FROM @TABLE1
WHILE @COUNT>=@Loop
BEGIN
DECLARE @VALUE VARCHAR(50)
SELECT @VALUE = VALUE From @TABLE1 Where mailto:ID=@Loop
IF NOT EXISTS (SELECT * FROM @TABLE2 Where VALUE = @VALUE)
BEGIN
INSERT INTO @TABLE2(VALUE)
VALUES (@VALUE)
END
SET @Loop = @Loop +1
END
SELECT * FROM @TABLE2

Please provide comments on the method defined. Also please enlighten me with any other approach that I have over looked.
Thursday, June 21, 2007
Retrieving Nth Salary in SQL Server 2000 and 2005
SQL Server 2000
Declare @Table Table
(
EID INT Identity,
ESalary INT
)
Insert into @Table(ESalary)
Values (400)
Insert into @Table(ESalary)
Values (4100)
Insert into @Table(ESalary)
Values (1400)
Insert into @Table(ESalary)
Values (2400)
Insert into @Table(ESalary)
Values (4300)
Select * From @Table [T1] Where
(2 = (Select Count(Distinct [ESalary]) From @Table [T2] where [T1]।[ESalary] <= [T2].[ESalary])) Just replace 2 by any digit 3,4,5। and it will work
SQL Server 2005
SQL Server 2005 provides a new function, Row_Number(), for generating row numbers, this function is used in conjunction with over for generating Ranks/ Rowids staring from 1 for the first row in each partition. I will be posting in more detail regading this feature in my next post. meanwhile you should be able to see how simple the job has become with SQL Server 2005
SELECT
*
FROM (
SELECT EID,ESalary,row_number() OVER (ORDER BY ESalary) AS Rank
FROM @Table
) t1
WHERE Rank=2
हेमंत गुप्ता
