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

Sunday, May 3, 2026

What is database and what are tables

 Table are also called relations.

Record is a row and also called tuple.

Table columns are also called attributes.


Saturday, March 7, 2026

How to add multiple insert values with for loop in SQL Server

 

    DECLARE @i INT =  1; -- Declare a variable for the loop counter

    DECLARE @max_value INT = 500; -- Set the loop limit

    -- Start the WHILE loop

    WHILE @i <= @max_value 

begin

        -- 3. The INSERT query inside the loop

         SET @i = @i + 1;

INSERT INTO [dbo].[AgentMaster]

           ([AgentName])

     VALUES

           ('test')

        end


  /****** Script for SelectTopNRows command from SSMS  ******/

SELECT [AgentNo]

      ,[AgentName]

  FROM [testdb].[dbo].[AgentMaster]

Monday, September 15, 2025

Monday, July 28, 2025

How to print a print statement of sql in asp.net c# - how to call a another event in asp.net event

 using System;

using System.Data.SqlClient;


public class SqlPrintExample

{

    public static void Main(string[] args)

    {

        string connectionString = "Data Source=YourServer;Initial Catalog=YourDatabase;Integrated Security=True";

        string sqlQuery = "PRINT 'This message comes from SQL.'; SELECT GETDATE() AS CurrentDateTime;";


        using (SqlConnection connection = new SqlConnection(connectionString))

        {

            // Attach an event handler to capture InfoMessage events (including PRINT statements)

            connection.InfoMessage += Connection_InfoMessage;

or// conn.InfoMessage += new SqlInfoMessageEventHandler( Connection_InfoMessage);

            try

            {

                connection.Open();

                using (SqlCommand command = new SqlCommand(sqlQuery, connection))

                {

                    using (SqlDataReader reader = command.ExecuteReader())

                    {

                        while (reader.Read())

                        {

                            Console.WriteLine($"Current Date Time from SQL: {reader["CurrentDateTime"]}");

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine($"Error: {ex.Message}");

            }

        }

    }


    private static void Connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)

    {

        // Display the message from the SQL PRINT statement

        Console.WriteLine($"SQL InfoMessage: {e.Message}");

    }

}

do while loop in sql server - Cursor in Sql Server

For data looping in sql Server, we use While loop  or cursor


 DECLARE @cnt INT = 0;


WHILE @cnt < 10

BEGIN

   PRINT 'Inside simulated FOR LOOP on TechOnTheNet.com';

   SET @cnt = @cnt + 1;

END;


PRINT 'Done simulated FOR LOOP on TechOnTheNet.com';

GO


For cursor:-


https://www.c-sharpcorner.com/blogs/how-to-use-cursors-and-while-loop-in-sql-server

Friday, May 30, 2025

A network-related or instance-specific error occurred while establishing a connection to SQL Server. Error

>First open services.msc from wondow + r (Window Services)

> Then right click on sql server (SQL express)

>go to Properties. 

Then go to Log On tab and select Local System account. 

Click on Ok button and start the service.

Friday, May 16, 2025

Restore of database failed sql server

>Open sql server

>Right Click on database >click on task>restore>database

>From device >select database bak file

>In option tab > Check overwrite the existing database (with replace)

or > in file tab click on reallocate all files to folder

Saturday, April 26, 2025

How to create unique key in SQL server management studio

 >First Open Visual Studio

>Right Click on table and click on Design

>Right Click on column name left side

>Click on Indexes/Key

>Select Unique key in 'Type' Filed


Note:- Sometime unique key option is not visible

Wednesday, March 26, 2025

How to write query with query builder in SQL server

 > Open Sql server management studio

> Right click on editor and click on "Design query in editor"

> And right click in that to change insert update or delete query.

How to create a new table with select query in SQL - INSERT RECORD FROM ONE TABLE TO ANOTHER - COPY TABLE

 SELECT   [Date]

      ,[SchemeNo] ,[Commission]

INTO        test3

FROM     PolicyDetails

Monday, May 13, 2024

SQL Server commands - To find columns from Table

 sp_help :- To get information about table and stored procedure

sp_help  'tablename'

 to get column name etc



sp_helptext  'storeprocedurename' 

will display stored procedure text 


To get Columns of table

SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('tablename') 



Find table and views in database

SELECT * FROM INFORMATION_SCHEMA.TABLES 

SELECT * FROM INFORMATION_SCHEMA.VIEWS

SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS  

EXEC sp_columns 'Employees'

ref:- https://stackoverflow.com/questions/578454/useful-system-stored-procedures-in-sql-server

Tuesday, May 7, 2024

How to join two tables without join and output as XML - SQL Server

 SELECT

    (SELECT d.DtFinalJudOrder FROM CourtApplicationDisposals d FOR XML PATH('CourtApplicationDisposalsList'), TYPE) AS CourtApplicationDisposals,

    (SELECT s.DtOfStay FROM CourtApplicationStayDetails s FOR XML PATH('CourtApplicationStayDetailsList'), TYPE) AS CourtApplicationStayDetails

FOR XML PATH(''), ROOT('root')



For single table with root

===========

SELECT  Doc.DocumentName, Doc.Subject, Doc.DocumentFilePath

FROM Document Doc

where Doc.ObjectType = 337 AND Doc.IsActive = 1

FOR XML RAW('DocumentList'),ELEMENTS,ROOT('Document')

Monday, May 6, 2024

Rename column in SQL Server table and change data type in SQL

 https://www.tutorialsteacher.com/sql/sql-modify-column-datatype-size

Union vs union all SQL server - Union SQL

Union will remove duplicates. Union All does not.

Note:- For union , must have same columns.

example

Union

Select top (select COUNT(*) from CourtApplicationDetails) * From CourtApplicationDetails

union 

Select top ((select COUNT(*) from CourtApplicationDetails)-(1)) * From CourtApplicationDetails

===============================

union all


Select top (select COUNT(*) from CourtApplicationDetails) * From CourtApplicationDetails

union all

Select top ((select COUNT(*) from CourtApplicationDetails)-(1)) * From CourtApplicationDetails