Table are also called relations.
Record is a row and also called tuple.
Table columns are also called attributes.
Table are also called relations.
Record is a row and also called tuple.
Table columns are also called attributes.
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]
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}");
}
}
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
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
>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.
>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
>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
> 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.
SELECT [Date]
,[SchemeNo] ,[Commission]
INTO test3
FROM PolicyDetails
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
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')
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