Pages

Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Thursday, July 26, 2012

How to compare two columns in a same table in a select statement and show a particular result ...

Hi Oracle fellas,

                        Some times in oracle developments, we have to compare various kinds of values, table columns and etc.. to give a comfortable out put for the users. In this process, some times we have to give them feedback by showing them some kind of status or a value putting to newly created column which is not in the related database table.
                        As an example, just imagine we have table name "Employees" and it include Emp_No, Emp_name, Previous_Year_Salary and Current_Year_Salary columns. We have to compare the previous salary with current salary and return a value as M if those two values are equal and NM if not equal. Apart from that we have to display above value in a separate column name "any name" which not include in Employees table. Just image that we are advised to not to add any new column to the related table.
All you have to do is write a simple SELECT statement and put two columns into decode function with two values you have to return accordingly. It's like,
SELECT  Emp_No, Emp_name, DECODE(Previous_Year_Salary , Current_Year_Salary, 'M', 'NM') 
FROM Employees;
Above statement will return all the fetched records with the relevant comparison and relevant newly added column values. You can put a name to new column like bellow.

SELECT  Emp_No, Emp_name, DECODE(Previous_Year_Salary , Current_Year_Salary, 'M', 'NM')  as 'any name'
FROM Employees;

Apart from this DECODE function includes lots of formulas we can use. Within DECODE function we can not only compare numbers, but also dates and characters as well.

Thursday, November 3, 2011

Running a 'for' loop in ms sql

I just want to insert few rows in to my table at once. It seems complicated for me to do that in a way i want. But i did stumble upon a convenient way for populating lot of new rows using a simple technique that is not using bulk insert. It is actually pretty basic stuff, but useful for my purpose.



DECLARE @count INT
SET @count = 0
WHILE (@count < 40)
BEGIN
   INSERT INTO some_table ([columnA], [columnB]) VALUES ('val1', 'val2')
   SET @count = (@count + 1)
END

All that was really done is a manual 'For' loop using the SQL WHILE loop.  Just set the number in WHILE (@count < 40) to however many times you want the loop to run. 

Tuesday, November 1, 2011

T-sql (Transact SQL) Reference (Database Engine)


Transact-SQL is central to using SQL Server. All applications that communicate with an instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user interface of the application.


The following is a list of the kinds of applications that can generate Transact-SQL:
  • General office productivity applications.
  • Applications that use a graphical user interface (GUI) to let users select the tables and columns from which they want to see data.
  • Applications that use general language sentences to determine what data a user wants to see.
  • Line of business applications that store their data in SQL Server databases. These applications can include both applications written by vendors and applications written in-house.
  • Transact-SQL scripts that are run by using utilities such as sqlcmd.
  • Applications created by using development systems such as Microsoft Visual C++, Microsoft Visual Basic, or Microsoft Visual J++ that use database APIs such as ADO,
    OLE DB,
    and ODBC.
  • Web pages that extract data from SQL Server databases.
  • Distributed database systems from which data from SQL Server is replicated to various databases, or distributed queries are executed.
  • Data warehouses in which data is extracted from online transaction processing (OLTP) systems and summarized for decision-support analysis.