SQL Server Function with Temp Table and Looping

ALTER FUNCTION [dbo].[fnGetPipelineProductsCarried]
 (
  @GIS_PipelineMasterId INT,
  @EnglishOrFrench NVARCHAR(20) = 'English'
 )
RETURNS VARCHAR(1000)
AS 
 BEGIN
  DECLARE @ReturnEnglish VARCHAR(1000) = ''
  DECLARE @ReturnFrench VARCHAR(1000) = ''
  DECLARE @ProductCarriedName NVARCHAR(255) = ''
  DECLARE @ProductCarriedNameFrench  NVARCHAR(255) = ''
  DECLARE @TempTable TABLE (
          PipelineName NVARCHAR(255),
          ProductCarriedName NVARCHAR(255),
          ProductCarriedNameFrench NVARCHAR(255),
          SUBABC NVARCHAR(5),
          PercentH2S DECIMAL(18,3),
       GIS_ProductCarriedTypeId INT,
       GIS_PipelineMasterId INT,
       DummyID INT IDENTITY(1,1) NOT NULL
  )

  INSERT INTO @TempTable  
  SELECT DISTINCT
      pm.PipelineName,
      pct.ProductCarriedName,
      pct.ProductCarriedNameFrench,
      pspc.SUBABC,
      pspc.PercentH2S,
      pspc.GIS_ProductCarriedTypeId,
      ps.GIS_PipelineMasterId
    FROM ((GIS_PipelineSegment ps
   INNER JOIN GIS_PipelineSegmentProductCarried pspc ON ps.GIS_PipelineSegmentId = pspc.GIS_PipelineSegmentId)
   INNER JOIN GIS_PipelineMaster pm ON ps.GIS_PipelineMasterId = pm.GIS_PipelineMasterId)
   INNER JOIN GIS_ProductCarriedType pct ON pspc.GIS_ProductCarriedTypeId = pct.GIS_ProductCarriedTypeId
   WHERE pm.GIS_PipelineMasterId = @GIS_PipelineMasterId
   ORDER BY pm.PipelineName,
      pspc.SUBABC;

  --Counters
  DECLARE @count INT
  DECLARE @count_max INT
  
  SET @count = 1
  SELECT @count_max = COUNT(DummyID) FROM @TempTable

  WHILE(@count <= @count_max)
  BEGIN
   SELECT @ProductCarriedName = ProductCarriedName,
             @ProductCarriedNameFrench = ProductCarriedNameFrench
     FROM @TempTable
    WHERE DummyID = @count
  
   SET @ReturnEnglish = @ReturnEnglish + @ProductCarriedName + ', '
   SET @ReturnFrench =  @ReturnFrench  + @ProductCarriedNameFrench + ', '
   
   --Must Always be at the end of the Loop
   SET @count = @count + 1
  END
  
  IF @EnglishOrFrench = 'English'
  BEGIN
   SET @ReturnEnglish = CASE @ReturnEnglish
                        WHEN NULL THEN NULL
                        ELSE (CASE LEN(@ReturnEnglish)
                              WHEN 0 THEN @ReturnEnglish
                              ELSE LEFT(@ReturnEnglish, LEN(@ReturnEnglish) - 1)
                              END )
                        END
 
   RETURN @ReturnEnglish
  END
  ELSE
  BEGIN
   SET @ReturnFrench = CASE @ReturnFrench
                       WHEN NULL THEN NULL
                       ELSE (CASE LEN(@ReturnFrench)
                             WHEN 0 THEN @ReturnFrench
                             ELSE LEFT(@ReturnFrench, LEN(@ReturnFrench) - 1)
                             END )
                       END

   RETURN @ReturnFrench
  END   
  
  RETURN @ReturnEnglish  
   
 END

OLE DB provider 'SQLNCLI' for linked server '' returned data that does not match expected data length for column '[server].[database].[dbo].[view]'. The (maximum) expected data length is <number>, while the returned data length is <number>.

The Actual Error for me was

Msg 7347, Level 16, State 1, Line 2

OLE DB provider 'SQLNCLI10' for linked server 'Common_Pts' returned data that does not match expected data length for column '[Common_Pts].[Pts].[dbo].[veRegulatoryInstrument].prefix'. The (maximum) expected data length is 4, while the returned data length is 5. 

After creating a linked server in ms sql express. and running the view I got the following error. to solve the problem run sp_refreshview command from the server that is hosting the view.

sp_refreshview [ @viewname = ] 'viewname' 
http://msdn.microsoft.com/en-us/library/ms187821.aspx
 

SQL Server: Can't remove an instance

I had an issue recently where I was not able to remove an instance of SQl Server, or so I thought. I kept on trying to remove the instance via Microsoft SQL Server 2014 RATHER THAN Microsoft SQL Server 2014 (64-bit). Once I selected the 64 bit option in Add-Remove Features I was able to finally gett rid of the instance I didn't want! Pretty dumb of me to not see the 64 bit option right away but there you go, sometimes you can't see the forest for the trees. 

Cannot connect to WMI provider. You do not have permission or the server is unreachable

This error usually happens if you have two SQL Server instances on the same box and someone removed one at some point.

http://support.microsoft.com/kb/956013

To work around this problem, open a command prompt, type the following command, and then press ENTER:

mofcomp "%programfiles(x86)%\Microsoft SQL Server\number\Shared\sqlmgmproviderxpsp2up.mof"

Note For this command to succeed, the Sqlmgmproviderxpsp2up.mof file must be present in the %programfiles(x86)%\Microsoft SQL Server\number\Shared folder.

The value of number depends on the version of SQL Server:nnn

 Collapse this tableExpand this table
Microsoft SQL Server 2012 110
Microsoft SQL Server 2008 R2 100
Microsoft SQL Server 2008 100
Microsoft SQL Server 2005 90

After you run the Mofcomp tool, restart the WMI service for the changes to take effect. The service name is Windows management Instrumentation.

SSRS Report site Security

The SSRS Reports website security can sometimes be troublesome when first configured and trying to connect to it from a remote machine. The RSReportServer.config file has a AuthenticationType section that required attention if you are prompted for authentication when accessing the site remotely. By default you may get all 3 types, however if you are just using regular Windows authentication you should only have RSWindowsNTLM in the list. Removing the other 2 will allow you into the site, provided you have access to the database.

 

      <AuthenticationTypes>
             <RSWindowsNegotiate />
             <RSWindowsKerberos />
             <RSWindowsNTLM />
      </AuthenticationTypes>

How to find a Field in a SQL Database

Replace Diameter with your own search.

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%Diameter%'
ORDER BY schema_name, table_name;

List ALL fields in a Database

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID 
ORDER BY schema_name, table_name;


Hope this helps.

How to remove “Server name” items from history of SQL Server Management Studio

For SQL 2005, delete the file:

C:\Documents and Settings\<USER>\Application Data\Microsoft\Microsoft SQL Server\90\Tools\Shell\mru.dat

For SQL 2008, the file location, format and name changed:

C:\Documents and Settings\<USER>\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

How to clear the list:

  • Shut down all instances of SSMS
  • Delete/Rename the file
  • Open SSMS