Pages

Social Icons

Wednesday, 30 January 2013

Find a string in whole database of SQL-Server

Hi,

I want to search a string in my database, Is there any way to do this?

Ans : yes, we can do this using Red Gate Search tool.

Can we do the same using SQL Query?

Now this is a tricky one but we can do this using a simple SQL-Query.


SELECT sm.object_id, OBJECT_NAME(sm.object_id) AS ObjectName, obj.type, obj.type_desc, sm.definition
FROM sys.sql_modules AS sm
JOIN sys.objects AS obj ON sm.object_id = obj.object_id
WHERE sm.definition LIKE '%YOUR_WORD%'

Thx,
RS


Monday, 28 January 2013

Difference between SQL-Server 2005/2008/2012

Difference between SQL-Server 2005 and SQL-Server 2008

1. In sql server 2005 we can not encrypt the database, while in sql server 2008 we can encrypt the entire database.

2. No table datatype is in sql-server 2005.

3. Central management system is introduced in 2008.

4. Policy based management(PBM) server is Introduced in sql 2008.

5. In sql server 2005,There is no option to compress backup files, but in sql server 2008.

6. Merge statement is introduced in sqlsever 2008

7. Value assignment at the time of vaiable declaration in SQL 2008

8. Ability to pass table variables into the stored procedures in SQL 2008


Difference between SQL-Server 2008 and SQL-Server 2012


1. The Maximum number concurrent connections to SQL Server 2008 is 32767. SQL server 2012 has unlimited concurrent connections.

2. The SQL Server 2008 uses 27 bit precision for spatial calculations. The SQL Server 2012 uses 48 bit precision for spatial calculations

3. TRY_CONVERT() and FORMAT() functions are not available in SQL Server 2008. 

4. ORDER BY Clause now have OFFSET / FETCH options to use paging to show required rows per page in applications and allow the user to scroll through each page of results rather than download the entire setIn the sample query below, SQL Server would return 10 records beginning with record 11. The OFFSET command provides a starting point for the SELECT statement in terms of paging, and the FETCH command provides how many records to return at a time. 

SELECT BusinessEntityID, FirstName, LastName FROM Person.Perso ORDER BY BusinessEntityID OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

5. Sequence is included in SQL Server 2012.Sequence is a user defined object that generates a sequence of a number. Here is an example using Sequence.

/****** Create Sequence Object ******/
CREATE SEQUENCE MySequence
START WITH 1
INCREMENT BY 1;

/****** Create Temp Table ******/
DECLARE @Person TABLE
(
ID int NOT NULL PRIMARY KEY,
FullName nvarchar(100) NOT NULL
);
/****** Insert Some Data ******/
INSERT @Person (ID, FullName) VALUES (NEXT VALUE FOR MySequence, 'Umar Ali'),
(NEXT VALUE FOR MySequence, 'John Peter'),
(NEXT VALUE FOR MySequence, 'Mohamed Iqbal');

/****** Show the Data ******/
SELECT * FROM @Person;

The results would look like this:
ID FullName
1 Umar Ali
2 John Peter
3 Mohamed Iqbal

6. The Full Text Search in SQL Server 2012 has been enhanced by allowing us to search and index data stored inextended properties or metadata. Consider a PDF document that has "properties" filled in like Name, Type, Folder path, Size, Date Created, etc. In the newest release of SQL Server, this data could be indexes and searched along with the data in the document itself. The data does have to be exposed to work, but it's possible now.

Tuesday, 22 January 2013

Hi,

In SQL query we say avoid to use of NOT Exist this is because of if we have index on our table then because of NOT Exist our query will not take the benefit of index. We can avoid the use of not exist using the below query.


In the above image i have shown the example of NOT EXIST and How to replace the NOT EXIST with LEFT JOIN.

Thanks,
Rahul



Friday, 18 January 2013

Remove splash screen of visual studio


Hi,

We can remove the splash screen of visual studio and can save 5 seconds each time when we open visual studio. For this we need to do only some things.

1. Click on Start>Right Click on visual studio icon>Click Properties.


2. In properties window go on target text-box, in the end type " -NOSPLASH"


3. Now hit the Apply and Ok button.

Thanks,
RS

Tuesday, 15 January 2013

SQL Server : Script for finding which variable is declared but never used


Hi,

Below query will help to get the list of all the variable which are declared in your sql code but never used.


-- Variable declared but never used Script
-- Original Author : Rahul Singi
DECLARE @str AS VARCHAR(MAX)=''
DECLARE @chrFound AS VARCHAR(255)
DECLARE @i AS INT=0
DECLARE @temp AS TABLE(colm1 VARCHAR(255))

SET @str=LOWER(@str)

DECLARE @NewLine AS CHAR(2)

SET @NewLine=char(13)+char(10)

WHILE @i<len(@str)
BEGIN
    IF((CHARINDEX('@',@str,@i)=0) OR (CHARINDEX(' ',@str,CHARINDEX('@',@str,@i))-CHARINDEX('@',@str,@i))<0)
    BEGIN
        BREAK;
    END
    
    SELECT @chrFound=SUBSTRING(@str,CHARINDEX('@',@str,@i),CHARINDEX(' ',@str,CHARINDEX('@',@str,@i))-CHARINDEX('@',@str,@i))
    
    INSERT INTO @temp
    SELECT rtrim(ltrim(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@chrFound,'!',''),'#',''),'$',''),'&',''),'=',''),')',''),';',''),@NewLine,'')))
    WHERE @chrFound NOT IN ('@@ERROR','@@IDENTITY','@@PACK_RECEIVED','@@ROWCOUNT','@@TRANCOUNT')
    
    SET @i=CHARINDEX(' ',@str,CHARINDEX('@',@str,@i))
    
    IF(CHARINDEX('@',@str,@i)=0)
    BEGIN
        BREAK;
    END
    
    PRINT(@i)
END

UPDATE @temp SET colm1=substring(colm1,0,charindex(char(9),colm1,0))
WHERE charindex(char(9),colm1,0)>0

SELECT colm1,'Variable declared but never used' AS Comment
FROM @temp
WHERE colm1 NOT IN(SELECT colm1
                   FROM @temp
                   GROUP BY colm1
                   HAVING COUNT(colm1)>1)



In the @str variable you need to give your sql code

Thx,
RS

Wednesday, 9 January 2013

Create minified version of java-script file


using System;
using System.IO;
using Yahoo.Yui.Compressor;
using System.Xml;
using System.Text;
using System.Linq;
using System.Collections.Generic;

namespace LCCA.Clinical.Tools
{
    public class Program
    {
        static void Main()
        {
            //Replace the project folder path as per your local environment
            const string projectDir = @"Paths"; // Local path where JavaScript files are stored in your machine
            const string operation = "mergejs";

            if (operation.ToLower().Equals("mergejs"))
            {
                CompressJS(projectDir);
                MergeJS(projectDir);
            }
            else if (operation.ToLower().Equals("compresscss"))
            {
                CompressCSS(projectDir);
            }
            else if (operation.ToLower().Equals("compressjs"))
            {
                CompressJS(projectDir);
            }
            else if (operation.ToLower().Equals("all"))
            {
                CompressCSS(projectDir);
                CompressJS(projectDir);
                MergeJS(projectDir);
            }
        }

        private static void MergeJS(string projectDir)
        {
            string scriptCatalog = Path.Combine(projectDir, "Scripts\\script-catalog.xml");
            string mergedFolderPath = Path.Combine(projectDir, "Scripts\\merged");
            string scriptsFolder = Path.Combine(projectDir, "Scripts");

            IList<string> scriptFiles =
                Directory.GetFiles(scriptsFolder, "*.js", SearchOption.AllDirectories).ToListIfNotNullOrEmpty();

            string content = File.ReadAllText(scriptCatalog);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(content);

            XmlNodeList files = doc.SelectNodes("//files/file");

            foreach (XmlElement file in files)
            {
                StringBuilder sb = new StringBuilder();

                XmlNodeList references = file.SelectNodes("reference");
                string targetFilePath = Path.Combine(mergedFolderPath, file.Attributes["name"].Value);
                foreach (XmlElement reference in references)
                {
                    string referenceFileName = reference.Attributes["name"].Value;

                    string referenceFilePath = scriptFiles.Single(x => new FileInfo(x).Name.Equals(referenceFileName));

                    string referenceContent = File.ReadAllText(referenceFilePath);

                    sb.AppendLine("//" + referenceFileName);
                    sb.AppendLine();
                    sb.AppendLine(referenceContent);
                    sb.AppendLine();
                }

                File.WriteAllText(targetFilePath, sb.ToString());
            }
        }

        private static void CompressJS(string projectDir)
        {
            string jsSourceFolder = Path.Combine(projectDir, "Scripts\\custom\\debug");
            string jsTargetFolder = Path.Combine(projectDir, "Scripts\\custom");
            CompressJS(jsSourceFolder, jsTargetFolder);
        }

        private static void CompressJS(string sourceFolder, string targetFolder)
        {
            foreach (string sourceFilePath in Directory.GetFiles(sourceFolder))
            {
                string sourceFilePathWithoutExtn = Path.GetFileNameWithoutExtension(sourceFilePath);
                string targetFilePath = Path.Combine(targetFolder, sourceFilePathWithoutExtn + ".min.js");
                if (new FileInfo(sourceFilePath).Extension.EqualsIgnoreCase(".js"))
                {
                    string content = File.ReadAllText(sourceFilePath);
                    string compressedContent = JavaScriptCompressor.Compress(content, true, true, false, false, 73);
                    File.WriteAllText(targetFilePath, compressedContent);
                }          
            }
        }

        private static void CompressCSS(string projectDir)
        {
            //First Run
            string cssSourceFolder = Path.Combine(projectDir, "Content\\css");
            string cssTargetFolder = Path.Combine(projectDir, "Content\\css");
            CompressCSS(cssSourceFolder, cssTargetFolder);

            //Second Run
            cssSourceFolder = Path.Combine(projectDir, "Content");
            cssTargetFolder = Path.Combine(projectDir, "Content");
            CompressCSS(cssSourceFolder, cssTargetFolder);
        }

        private static void CompressCSS(string sourceFolder, string targetFolder)
        {
            foreach (string sourceFilePath in Directory.GetFiles(sourceFolder))
            {
                if (new FileInfo(sourceFilePath).Extension.EqualsIgnoreCase(".css"))
                {
                    string sourceFilePathWithoutExtn = Path.GetFileNameWithoutExtension(sourceFilePath);
                    string targetFilePath = Path.Combine(targetFolder, sourceFilePathWithoutExtn + ".css");
                    string content = File.ReadAllText(sourceFilePath);
                    string compressedContent = CssCompressor.Compress(content, 73, CssCompressionType.StockYuiCompressor);
                    File.WriteAllText(targetFilePath, compressedContent);
                }
            }
        }
    }
}

SSRS Interview Questions II

1. What does rdl stand for?
    RDL stand for Report Definition Language. RDL is the extension for reports file.

2. What is Report Manager?
    Report Manager is a web based tool that allows to access and run reports.

3. What is Report Builder?
    Report Builder is a self-service tool for end users

4. What permission do you need to give to users to enable them to use Report Builder?
    "Report Builder" role and "system user". Report builder should also be enable in report server properties

5. What do you need to restore report server database on another machine?
    SSRS Encryption key

6. How to pass multi-valued parameter to stored procedure in dataset?
    Join function in SSRS and split function in T-SQL

7. How to create "dependent" parameter "Make, Model, Year"
    They need to be in correct order, and previous parameter should filter next parameter dataset. For instance Model dataset should be filtered using Make parameter

8. How to create "alternate row colour"?
    IIF(ROWNUMBER(NOTHING)%2=0,"White","White Smoke")

9. How to create percentile function?
    Custom code is required for this
    Code : 
       Public Function Percent(ByVal CurrVal As Long, ByVal MaxVal As Long) As Decimal
                 If CurrVal = 0  or MaxVal = 0 Then
                       Return 0
                 Else
                       Return (CurrVal / MaxVal * 100)
                 End If
        End Function

10. How to find a value in another dataset based on current dataset field (SSRS 2008 R2)?
      using lookup function
      Example : =JOIN(LookupSet(Fields!DepartmentID.Value, Fields!ID.Value, Fields!Name.Value, "Departments"),","

11. How to identify current user in SSRS Report?
      User!UserID

12. What is the shared data source in ssrs?
      A shared data source is a set of data source connection properties that can be referenced by multiple reports, models, and data-driven subscriptions that run on a Reporting Services report server. Shared data sources provide an easy way to manage data source properties that often change over time. If a user account or password changes, or if you move the database to a different server, you can update the connection information in one place.
Shared data sources are optional for reports and data-driven subscriptions, but required for report models. If you plan to use report models for ad hoc reporting, you must create and maintain a shared data source item to provide connection information to the model.
13. What is the report rendering?
     Report rendering is to call the report from server to application. Report rendering can be possible through different ways.