SQL Server 2022 : T-SQL बुनियादी उपयोग2023/12/07 |
यह Transact-SQL (T-SQL) के उपयोग का कुछ बुनियादी उदाहरण है।
|
|
[1] | डेटाबेस बनाएँ/हटाएँ। |
root@dlp:~# sqlcmd -S localhost -U SA Password: # [SampleDB] डेटाबेस बनाएं 1> create database SampleDB; 2> go # मापदंडों के साथ एक डेटाबेस बनाएं 1> create database SampleDB2 2> on primary ( name = 'SampleDB2', 3> filename = '/var/opt/mssql/data/SampleDB2.mdf', 4> size = 5GB, maxsize = unlimited, filegrowth = 10MB ) 5> log on ( name = 'SampleDB2_log', 6> filename = '/var/opt/mssql/data/SampleDB2_log.ldf', 7> size = 1GB, maxsize = 2GB, filegrowth = 5% ) 8> go # सूची डेटाबेस 1> select name,create_date from sys.databases; 2> go name create_date ------------------------ ----------------------- master 2003-04-08 09:13:36.390 tempdb 2023-11-24 09:28:30.890 model 2003-04-08 09:13:36.390 msdb 2023-10-30 16:52:55.553 SampleDB 2023-11-24 09:51:11.083 SampleDB2 2023-11-24 09:54:12.950 (6 rows affected) # [SampleDB2] डेटाबेस हटाएँ 1> drop database SampleDB2; 2> go |
[2] | तालिकाएँ बनाएँ/हटाएँ। |
# डेटाबेस के साथ SQL सर्वर से कनेक्ट करें root@dlp:~# sqlcmd -S localhost -U SA -d SampleDB Password: # [Sample_Table] तालिका बनाएं 1> create table dbo.Sample_Table ( 2> Number nvarchar(10) not null, 3> First_Name nvarchar(50) not null, 4> Last_Name nvarchar(50) null, 5> Last_Update date not null ) 6> go # सूची तालिकाएँ 1> select name from sysobjects 2> where xtype='u' 3> go name ----------------------------------- Sample_Table (1 rows affected) # [Sample_Table] तालिका हटाएँ 1> drop table dbo.Sample_Table; 2> go |
[3] | डेटा डालें/अपडेट करें/हटाएं। |
root@dlp:~# sqlcmd -S localhost -U SA -d SampleDB Password: # डेटा डालें 1> insert into dbo.Sample_Table ( Number, First_Name, Last_Name, Last_Update ) 2> values ( '00001', 'Ubuntu', 'Linux', '2023-11-24' ), 3> ( '00002', 'Debian', 'Linux', '2023-11-24' ), 4> ( '00003', 'RedHat', 'Linux', '2023-11-24' ) 5> go (3 rows affected) 1> select * from dbo.Sample_Table; 2> go Number First_Name Last_Name Last_Update ---------- --------------- ---------------- ---------------- 00001 Ubuntu Linux 2023-11-24 00002 Debian Linux 2023-11-24 00003 RedHat Linux 2023-11-24 (3 rows affected) # निर्दिष्ट कॉलम के साथ तालिकाएँ दिखाएँ 1> select Number, First_Name from dbo.Sample_Table; 2> go Number First_Name ---------- -------------------------- 00001 Ubuntu 00002 Debian 00003 RedHat (3 rows affected) # शीर्ष 2 डेटा दिखाएँ 1> select top 2 * from dbo.Sample_Table; 2> go Number First_Name Last_Name Last_Update ---------- ----------------- ---------------- ---------------- 00001 Ubuntu Linux 2023-11-24 00002 Debian Linux 2023-11-24 (2 rows affected) # अद्यतन आकड़ें 1> update dbo.Sample_Table set Last_Update = '2023-11-25' where First_Name = 'Ubuntu' 2> go (1 rows affected) 1> select * from dbo.Sample_Table where First_Name ='Ubuntu'; 2> go Number First_Name Last_Name Last_Update ---------- ---------------- -------------- ---------------- 00001 Ubuntu Linux 2023-11-25 (1 rows affected) # डेटा हटाएं 1> delete dbo.Sample_Table where First_Name ='Ubuntu'; 2> go (1 rows affected) 1> select * from dbo.Sample_Table where First_Name ='Ubuntu'; 2> go Number First_Name Last_Name Last_Update ---------- ------------- ------------- ---------------- (0 rows affected) |
[4] | निम्न प्रकार से T-SQL को सीधे चलाना भी संभव है। |
root@dlp:~# sqlcmd -S localhost -U SA -Q 'select name,create_date from sys.databases' Password: name create_date -------------------------- ----------------------- master 2003-04-08 09:13:36.390 tempdb 2023-11-24 09:28:30.890 model 2003-04-08 09:13:36.390 msdb 2023-10-30 16:52:55.553 SampleDB 2023-11-24 09:51:11.083 SampleDB2 2023-11-24 10:02:21.497 (6 rows affected) |
Sponsored Link |
|