Thursday, June 30, 2011

Character Associated With Each Number

WITH NUMBERS AS (




SELECT 0 AS NUMBER UNION ALL SELECT NUMBER + 1 FROM NUMBERS WHERE NUMBER + 1 <= 255 ) SELECT NUMBER, CHAR(NUMBER) AS CHARACTER FROM NUMBERS OPTION (MAXRECURSION 255);

Tuesday, June 21, 2011

Comma Value Seprater

CREATE FUNCTION [dbo].[ufn_CSVToTable] ( @StringInput VARCHAR(8000) )
RETURNS @OutputTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN

    DECLARE @String    VARCHAR(10)

    WHILE LEN(@StringInput) > 0
    BEGIN
        SET @String      = LEFT(@StringInput,
                                ISNULL(NULLIF(CHARINDEX(',', @StringInput) - 1, -1),
                                LEN(@StringInput)))
        SET @StringInput = SUBSTRING(@StringInput,
                                     ISNULL(NULLIF(CHARINDEX(',', @StringInput), 0),
                                     LEN(@StringInput)) + 1, LEN(@StringInput))

        INSERT INTO @OutputTable ( [String] )
        VALUES ( @String )
    END
   
    RETURN
END
GO

How to Know if a column have identity set on it

SELECT OBJECT_NAME(O.OBJECT_ID) AS TABLENAME,
IC.NAME AS COLUMNNAME,
SEED_VALUE,
INCREMENT_VALUE,
LAST_VALUE,
IS_NOT_FOR_REPLICATION
FROM SYS.IDENTITY_COLUMNS IC
INNER JOIN SYS.OBJECTS O ON O.OBJECT_ID = IC.OBJECT_ID
WHERE O.NAME = ''
ORDER BY 1
----------------------------------
select TABLE_NAME + '.' + COLUMN_NAME AS COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'DBO'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') =1
order by TABLE_NAME

Wednesday, June 15, 2011

Different Between SP and Function SQL-Server


1. Functions are compiled and executed at run time.
Stored procedures are stored in parsed and compiled format in the database.

2. Functions cannot affect the state of the database which means we cannot perform insert,delete,update and create operations on the database.
Stored Procedures can affect the state of the database by using insert,delete,update and create operations.

3 Functions are basically used to compute values. We passes some parameters to functions as input and then it performs some operations on the parameter and return output.
Stored procedures are basically used to process the task.

4.Function can not change server environment and our operating system environment.
Stored procedures can change server environment and our operating system environment.

5. Procedures can have input,output parameters for it whereas functions can have only input parameters.
6. Procedure can return zero or n values whereas function can return one value which is mandatory.
7. Functions can be called from procedure whereas procedures cannot be called from function.
8. Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.
9. Procedures can not be utilized in a select statement whereas function can be embedded in a select statement