Pages

Showing posts with label SqlServer. Show all posts
Showing posts with label SqlServer. Show all posts

Thursday, 27 February 2014

Add/Copy Rows from One Datatable to Another Datatable C#

Add/Copy Rows from One Datatable to Another Datatable C#

C# 
 
DataTable _dt=new DataTable();
_dt = _ds.Tables[0];
DataTable dt1 = ds1.Tables[0];
for (int i = 0; i < dt1.Rows.Count; i++)
{
_dt.ImportRow(dt1.Rows[i]);
}

VB

Dim _dt As New DataTable()
_dt = _ds.Tables(0)
Dim dt1 As DataTable = ds1.Tables(0)
For i As Integer = 0 To dt1.Rows.Count - 1
_dt.ImportRow(dt1.Rows(i))
Next


Difference between View and Stored Procedure

Difference between View and Stored Procedure

 View in SQL Server

A view represents a virtual table. By using view we can join multiple tables and present the data as coming from a single table.

For example consider we have two tables

      1)    UserInformation table with columns userid, username
      2)    SalaryInformation table with columns salid, userid, salary

Create VIEW by joining above two tables 
 
CREATE VIEW VW_UserInfo
AS
BEGIN
SELECT a.userid,a.username,b.salary from UserInformation a INNER JOIN SalaryInformation b ON a.userid=b.userid
END

 CREATE PROCEDURE GetUserInfo
@uid INT
AS
BEGIN
SELECT username from VW_UserInfo WHERE userid=@uid
END

Stored Procedure

 A stored procedure is a group of sql statements that has been created and stored in the database. Stored procedure will accept input parameters so that a single procedure can be used over the network by several clients using different input data. Stored procedure will reduce network traffic and increase the performance. If we modify stored procedure all the clients will get the updated stored procedure

 USE AdventureWorks2008R2;
GO
CREATE PROCEDURE dbo.sp_who
AS
    SELECT FirstName, LastName FROM Person.Person;
GO
EXEC sp_who;
EXEC dbo.sp_who;
GO
DROP PROCEDURE dbo.sp_who;
GO

Thursday, 13 February 2014

How SQL Server Add Auto Increment Column or Set Auto Increment Column SQL Server Management Studio

How SQL Server Add Auto Increment Column or Set Auto Increment Column SQL Server Management Studio








Demo



<b>UserId</b> <b>UserName</b> <b>FirstName</b> <b>LastName</b> <b>Email</b> 1 suranisizar sizar surani fantasysasp.net@gmail.com





CREATE TABLE UserDetails ( UserId int PRIMARY KEY IDENTITY, UserName varchar(200), FirstName varchar(255), LastName varchar(255), Email varchar(255) )


How SQL Server Reset Identity Column Value to 1 in SQL Database

How SQL Server Reset Identity Column Value to 1 in SQL Database





To reset identity column value and start value from “1” during insert new records we need to write query to reset identity column value. Check below Query



DBCC CHECKIDENT (Table_Name, RESEED, New_Reseed_Value)


Table_Name is name of your identity column table
RESEED specifies that the current identity value should be changed.
New_Reseed_Value is the new value to use as the current value of the identity column.



<b>EX</b>: DBCC CHECKIDENT ('UserDetails', RESEED, 0)



Sunday, 2 February 2014

How to Get Database Size in SQL Server 2008

How to Get Database Size in SQL Server 2008 /



SELECT database_name = DB_NAME(database_id) , log_size_mb = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2)) , row_size_mb = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2)) , total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2)) FROM sys.master_files WITH(NOWAIT) WHERE DB_NAME(database_id) = 'master' –-- your database name GROUP BY database_id


Demo:






How to Get Table Size in SQL Server Query to Get Table Size in SQL Server 2008

How to Get Table Size in SQL Server  Query to Get Table Size in SQL Server 2008



SP_SPACEUSED 'YOUR TABLE NAME'    



SP_SPACEUSED 'USERDETAILS'    



Demo:





How to Get List of Tables in Database in SQL Server 2008

How to Get List of Tables in Database in SQL Server 2008 /



USE SampleDB  SELECT * FROM information_schema.tables         
             OR       
USE SampleDB  SELECT * FROM SYS.tables      



Demo:






Saturday, 1 February 2014

How SQL Server Get Stored Procedure Last Modified Date or Updated Date in SQL Server

How SQL Server Get Stored Procedure Last Modified Date or Updated Date in SQL Server 


SELECT name,   create_date, modify_date FROM sys.objects WHERE type = 'P'  








SELECT name, create_date, modify_date FROM sys.objects WHERE type = 'P' AND name like '%your procedure name%'





Thursday, 4 July 2013

How To Add Identity Property to Existing Column in Table in SQL Server

How To Add Identity Property to Existing Column in Table in SQL Server

Create Table As Below

CREATE TABLE UserDtls

(

UserId int PRIMARY KEY,

UserName varchar(120),

Qualification varchar(50)

)

Now, Insert Data As Below

INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(1,'sizar','Be')

INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(2,'sahil','Be')

INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(3,'salim','Be')

The Table Is As Follow 

How To Add Identity Property to Existing Column in Table in SQL Server
How To Add Identity Property to Existing Column in Table in SQL Server

We can create identity column with above method only whenever we don't have any data in table otherwise we need to use T-SQL query for that follow below steps

1. Create another table(temp1) with same structure as old table(UserDtls) table with identity column.

2. Now move the data from UserDtls table to temp1 table for that you need to ON Identity insert property to know more about it check this article insert values in identity column in SQL.

3. Once inserted drop original table UserDtls and rename temp1 to UserDtls. For above steps below is the code we need to run to create identity column for existing table

     ---- Create New Table with Identity Column ------
CREATE TABLE temp1
(
UserId INT PRIMARY KEY IDENTITY,
UserName VARCHAR(120),
Qualification VARCHAR(50)
)
----Insert Data into newly created table----------
SET IDENTITY_INSERT temp1 ON
IF EXISTS(SELECT TOP 1 * FROM UserDtls)
BEGIN
INSERT INTO temp1(UserId,UserName,Qualification)
SELECT UserId,UserName,Qualification FROM UserDtls
END
SET IDENTITY_INSERT temp1 OFF
--------Once Data moved to new table drop old table --------
DROP TABLE UserDtls
-------Finally rename new table name to old table name
EXEC sp_rename 'temp1','UserDtls'         


By using above method we can add identity property to existing column in table using using SQL Server.