Monday, December 14, 2009

Creating a table from your view

     In one of my projects had to create a table and populate it with my view.  You can do this as :

SELECT *
INTO dbo.tbl_tblname
FROM dbo.vw_viewname


Saturday, October 10, 2009

Find the port SQL Server is running

 By default the TCP Port for SQL Server is 1433 , and the USD connection is 1434 . If you have a named instance the TCP port is configured dynamically .

You can find out this information by going to the registry and looking up TCP settings.

SQL 2005
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.<InstanceNumber>\MSSQLServer\
SuperSocketNetLib\TCP\

SQL 2008
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.<InstanceName>\MSSQLServer\
SuperSocketNetLib\TCP\

An alternate easier way would be the SQL Server Configeration Manager UI

Go to Windows > Start > SQL Server Program Folder > SQL Server Configuration Manager > SQL Server Network Configurations >  Protocols for your SQL Server > TCP/IP and right click the link.

Friday, June 12, 2009

SQL function to split a multivalue parameter


       In reports with multivalue parameters, the input parameter can be passed as a list to the sql server procedure where we can use a UDF to split it.
For ex if we a have a procedure to return employee names for multiple employee ids then the proc will look something like this

CREATE PROC [dbo].[proc_EmpName_getAll]
@EmpIDKey VARCHAR(1000)
AS
SELECT   EmpIDKey, EmpName FROM dbEmployee
      WHERE EmpIDKey IN (SELECT Item FROM  dbo.Split(@EmpIDKey, ',') AS Split_1)
ORDER BY EmpName

  The  sql function below takes a user defined multi dimensional object and a delimitter character as input parameters and breaks them into individual items. 

CREATE FUNCTION [dbo].[Split]/* This function is used to split up multi-value parameters */(
@ItemList NVARCHAR(4000),
@delimiter CHAR(1)
)
RETURNS @IDTable TABLE (Item NVARCHAR(50) collate database_default )
AS
BEGIN


     DECLARE
@tempItemList NVARCHAR(4000)
     SET @tempItemList = @ItemList

     DECLARE @i INT
     DECLARE @Item NVARCHAR(4000)

     SET @tempItemList = REPLACE (@tempItemList, @delimiter + ' ', @delimiter)
     SET @i = CHARINDEX(@delimiter, @tempItemList)

     WHILE (LEN(@tempItemList) > 0)
     BEGIN
          IF
@i = 0
            SET @Item = @tempItemList
         ELSE
            SET @Item = LEFT(@tempItemList, @i - 1)
         INSERT INTO @IDTable(Item) VALUES(@Item)

         IF @i = 0
             SET @tempItemList = ''
         ELSE
             SET
@tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i)
             SET @i = CHARINDEX(@delimiter, @tempItemList)
         END
   RETURN
END

Thursday, May 14, 2009

SSRS - Custom code embedded in your report


In Reporting Services you will often need  to manipulate or dynamically format the  report data .  The built in  expressions  can do a lot of these, but most often you might want to format data or calculate values for which there are no expression readily available. This is where the built in custom code comes.
In one of my reports I had to calculate the Next month, having the current month and year as my input parameters.
You write your custom code in the report properties code window. Go to the report layout Select                                         Report ->  Report Properties  -> Code
        Public Function getNextDate(ByVal month As String, ByVal year As String) As String
Try
Dim dateNow As DateTime
dateNow   =month+ "-01-" + year
Dim nextMonth As DateTime
nextMonth  = dateNow  .AddMonths(1)
Return nextMonth

Catch ex As exception
Return ex.message
End Try
End Function

Now in the textbox where you want the next month displayed call the custom code 
="Next Month is  "+ code. getNextDate (Parameters!Month.Value,Parameters!Year.Value)





Tuesday, April 7, 2009

ISNULL()

The ISNULL  function in SQL Server is used to substitute alternate value if the one being checked is “NULL”. 
Take the following example.


DECLARE @var1 VARCHAR(50) 
DECLARE @var2 VARCHAR(5) 

SET @var1 = 'Hello'
SET @var2= NULL

SELECT ISNULL(@var2,@var1)

Your output is Hello

Now try substituting @var1 with a bigger sentence
SET @var1 = 'Hello World Its great to be here'

Now your output is just Hello


This is because the ISNULL funnction type casts var2 to var1
Try the following, it will resolve the issue

SELECT ISNULL(CONVERT(VARCHAR(50), @var2),@var1)