Posts

Showing posts with the label SQL

SQL – Generate decimal numbers sequence for particular number range

Image
Friends, Sometimes we need mathematical assistance in SQL for such queries. Same here, it helps me to find challenging in all the way. Some of my friends helps me out to crack the records for my query, Here I am sharing simple solution for this, ;WITH cte      AS (SELECT Cast(-77 AS NUMERIC(22, 3)) AS num –Min value from your data          UNION ALL          SELECT Cast(num + 0.00001 AS NUMERIC(22, 5))          FROM   cte          WHERE  num < Cast(89 AS NUMERIC(22, 5))) — Max value from your data SELECT * FROM   cte OPTION (maxrecursion 0)  Try it 

SQL SERVER – Using PowerShell and Native Client to run queries in SQL Server

Image
Many time I heard about Powershell , but never go thorogh it. I tried it at basic level to connect to sql server from SQL Native Client. Coding looks similar to web developers  (like some properties as  'ExecuteScalar' ,'ExecuteNonQuery' ). Its very interesting which comes with SSMS (SQL Server Management Studio) . I already have sample database created on my local , which I would like to connect through powershell. /*Create table and keep some records for fetching from SQL Query*/ CREATE TABLE [dbo].[tbl_Employee](  [ID] [int] IDENTITY(1,1) NOT NULL,  [FirstName] [varchar](max) NULL,  [LastName] [varchar](max) NULL,  [Address] [varchar](max) NULL)   "1" indicates record inserted successfully !!    PowerShell very useful any kind of deployment on remote servers  .   Share your comments with addition information . Hope u will try it . You might get few error , but its interesting.

Ways to SELECT XML string using Nodes and OPENXML()

XML reading is most probably way of reading data from file. Only for knowledge we are checking performance by two ways OPENXML() and Nodes(). Check following the queries at your end as part of "Performace Tuning ". OpenXML() -                       OpenXML() is a rowset provider. OpenXML can be used in T-SQL statements in which rowset providers such as a table, view, or the OPENROWSET function can appear. Example : DECLARE @idoc int , @doc varchar ( 1000 ), @XMLString xml SET @doc = ' <ROOT> <Customer CustomerID="1" ContactName="Nihar"> <Order CustomerID="1" EmployeeID="5" OrderDate="1996-07-04T00:00:00"> <OrderDetail ProductID="11" Quantity="12"/> <OrderDetail ProductID="42" Quantity="10"/> </Order> </Customer> <Customer CustomerID="2" ContactName="HR"> <Order CustomerID=...

select count(*) vs count(1) in sql server database

COUNT(SomeColumn) will only return the count of rows that contain non-null values for SomeColumn.  COUNT(*) and COUNT('Foo') will return the total number of rows in the table. Other else no such big difference between them.....  

ALTER TABLE STATEMENTS

SQL ALTER TABLE Syntax Add a column in a table , use the following syntax:         ALTER TABLE table_name         ADD column_name datatype   Delete a column in a table , use the following syntax (notice that some database systems don't allow deleting a column):          ALTER TABLE table_name         DROP COLUMN column_name   Change the data type of a column in a table , use the following syntax: 1) My SQL / SQL Server / MS Access: ALTER TABLE table_name ALTER COLUMN column_name datatype   2) Oracle: ALTER TABLE table_name MODIFY column_name datatype     You can try it...  Hopes it will helps you