Posts

Showing posts with the label SQL

Insert Records to SQl Temp Table

SELECT * INTO # TempTable FROM OriginalTable

Find Specific Text on Stored Procedure

Image
Do you remember we had so many requirements to check a text on stored procedures or a view? Think that we are going to change a field name of a table or a data type of a field. Or we are going to replace or delete a data table in your database. Database might be so big and there may be hundreds of stored procedures. We can't open every single stored procedures or a view and check whether they are using the data table we going to delete. For that, We can simply find the stored procedures which are using this particular data table or a field. That may help us to reduce the work load more than 80%. You can use this following query for it. SELECT     * FROM       INFORMATION_SCHEMA . ROUTINES WHERE      ROUTINE_DEFINITION LIKE '%TableName%'

Create Summary view of SQL Datatable

Combine two rows into one row with same ID Go through the following SQL table named [EmployeeSales] . Think that you have to create a summary table of monthly sales against each employee ID.  EmpID Year Month Sales 0001 2012 May 0.5 0001 2012 June 0.8 0001 2012 July 0.7 0002 2012 May 1.1 0002 2012 June 0.9 0003 2012 May 0.4 0003 2012 June 0.5 0003 2012 July 0.5 [EmployeeSales]  Table For that You can write a SQL query as follows. In that Query I have filter sales records relate to year 2012 and sord according to EmpID .  SELECT  EmpID ,   MAX ( CASE   WHEN  [Month]  =   'May'  THEN  [Sales]  ELSE   NULL   END )   AS  [May] ,   MAX ( CASE   WHEN  [Month]  =   'June...