Showing posts with label sql server. Show all posts
Showing posts with label sql server. Show all posts

Thursday, April 28, 2022

Sql Server: Lấy dòng dữ liệu ngẫu nhiên

(Anhgolden's Blog) - Khi cần lấy dòng dữ liệu ngẫu nhiên, ta dùng câu lệnh sql như sau:
Select *
From [Table]
Order by NewID()

Read More »

Friday, July 30, 2021

Error: String or binary data would be truncated

(Anhgolden's Blog) - Lỗi này sảy ra khi lưu dữ liệu trên CSDL (DB) vượt quá giới hạn của Cột.

The "String or binary data would be truncated" error indicates that the procedure is attempting to store something in the DBServerInfo table that is larger than the column allows. The two known reasons this can occur are:

1) SQL Server has at least one database whose name exceeds 25 characters in length.

2) The 'show advanced options' parameter is enabled in SQL Server and you are running the Enterprise edition of SQL Server.
Read More »

Thursday, October 26, 2017

Using Alias in Update SQL Statement

(Anhgolden's Blog) - Example

Without Alias:
==//==
Update tableA
Set name='abc'
==//==

With Alias statement:

Update A
Set A.name='abc'
From tableA as A

Update A
Set A.name=B.name
From tableA as A, tableB as B
Where A.id=B.id


Read More »

Wednesday, September 6, 2017

SQL Server: Phân quyền cho User

(Anhgolden's Blog) - Để phân quyền cho User sử dụng trong SQL Server, ta sử dụng các câu lệnh sau:

a) Phân quyền:

Grant All on TableName/ViewName to UserName

Hoặc

Grant Select [,Insert] [,Update][,Delete] on TableName/ViewName to UserName

b) Xóa bỏ phân quyền:

Revoke All on TableName/ViewName from UserName
Read More »

Wednesday, May 17, 2017

Functions: Excel vs MS Access vs SQL Server

(Anhgolden's Blog) - Tổng hợp

1. Hàm tìm kiếm chuỗi (Search or Find): Trả kết quả False hoặc Vị trí tìm được. Riêng SQL Server kết quả từ 0 đến N (Vị trí tìm được).

Excel: =Find(Str_searched, InString, [Start])

MS Access: InStr([Start], InString, Str_searched)

SQL Server: CharIndex(Str_searched, InString, [Start])

2. Hàm chuyển đổi chuỗi số sang số (Text to number):

Excel: =Value(text)

MS Access: Val(text)

SQL Server: Convert(Int, text)

3. Hàm lấy 1 phần tử trong chuỗi

Excel: Mid(String,Start,Length)

MS Access: Mid(String,Start,Length)

SQL Server: SubString(String,Start,Length)

4. Hàm điều kiện

Excel: IF(conditions, result-true, result-false)

MS Access: IIF(conditions, result-true, result-false)

SQL Server:

CASE expression

   WHEN value_1 THEN result_1
   WHEN value_2 THEN result_2
   ...
   WHEN value_n THEN result_n

   ELSE result

END

Hoặc

CASE

   WHEN condition_1 THEN result_1
   WHEN condition_2 THEN result_2
   ...
   WHEN condition_n THEN result_n

   ELSE result

END

4. Hàm lấy ngày hiện tại
Excel: today()
Access: date()
Sql Server: getdate()


Read More »

Tuesday, April 19, 2016

SQL Server: Tổng hợp

(Anhgolden's Blog) - Tổng hợp

1. Primary Key Vs Unique:

- Primary Key (khóa chính) bản thân đã bao gồm tính Unique (dữ liệu duy nhất và không trống).
- 1 Table chỉ có 1 Primary Key, nhưng có thể có nhiều field Unique.

2. Primary Key Vs Index:

- Khi thiết lập Primary Key hay Index dữ liệu đã được sắp xếp (Sorting), giúp cho việc truy vấn dữ liệu được nhanh chóng.
- Tuy nhiên Primary Key sẽ check tính duy nhất Unique (Not Null) và 1 table chỉ có 1 Primary key, còn Index có thể thêm thuộc tính Unique (On/Off) và 1 table có thể có nhiều Index cho nhiều field.

3. Group by Vs Distinct:

- Group by và Distinct có cùng điểm chung là loại bỏ dòng dữ liệu trùng. Tuy nhiên, Group by được sử dụng khi có yếu tố tính gộp.

Ví dụ:

Example of DISTINCT:
SELECT DISTINCT Employee, Rank
FROM Employees

Example of GROUP BY:
SELECT Employee, Rank
FROM Employees
GROUP BY Employee, Rank

Example of GROUP BY with aggregate function:
SELECT Employee, Rank, COUNT(*) EmployeeCount
FROM Employees
GROUP BY Employee, Rank
4. Add/Drop/Modify Column:

ALTER TABLE table_name
ADD column_name datatype

ALTER TABLE table_name
DROP COLUMN column_name

ALTER TABLE table_name
ALTER COLUMN column_name datatype



ALTER TABLE table_name
ALTER COLUMN column_name datatype NULL

ALTER TABLE table_name
ALTER COLUMN column_name datatype NOT NULL

ALTER TABLE table_name
ADD Primary Key (column_name)

ALTER TABLE table_name
ADD Constraint PK_key_name Primary Key (ID,column_name)

ALTER TABLE table_name
ADD CONSTRAINT UC_key_name UNIQUE (ID,column_name);

ALTER TABLE table_name
ADD Constraint Pf_key_name default getdate() for column_name

ALTER TABLE table_name
ADD Constraint ID int identity(1,1) NOT NULL

CREATE TABLE Table_sample (         ID int identity(1,1) NOT NULL,
Ngaycapnhat datetime DEFAULT GETDATE(),
        Sotien Decimal(25,0)
);





Read More »