Post by Goborijung at 2023-07-12 09:48:56 | ID: 1949
Declare @tblName varchar(100) = 'PDK1_M_PAOUNDAM'-- หา Index
exec sp_helpindex @tblName -- หรือ /* select * from sys.indexes where object_id = (select object_id from sys.objects where name = 'PDK1_M_PAOUNDAM') And name is not null */-- หา Primary Key
exec sp_pkeys @tblName -- หรือ /* SELECT Col.Column_Name from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col WHERE Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Tab.Constraint_Type = 'PRIMARY KEY' AND Col.Table_Name = 'PDK1_M_PAOUNDAM' */-- หา Size Table
EXEC sp_spaceused @tblName-- หา Dependency
SELECT name,type,type_desc,referenced_server_name,referenced_database_name,referenced_schema_name,referenced_entity_name FROM sys.sql_expression_dependencies A, sys.objects B WHERE referenced_id = OBJECT_ID(@tblName) AND A.referencing_id = B.object_id order by name --หรือ /* select B.name ,B.type, B.type_desc, A.referenced_server_name, A.referenced_database_name ,A.referenced_schema_name ,A.referenced_entity_name from sys.sql_expression_dependencies A inner join sys.objects B ON A.referencing_id = B.object_id Where A.referenced_entity_name = 'PDK1_M_PAOUNDAM' order by B.name */
Post by Goborijung at 2023-12-01 14:30:43 | ID: 1993
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Post by Goborijung at 2024-08-28 15:38:05 | ID: 2017
Declare @tblName varchar(100) = 'tblName'
Select ROW_NUMBER() over(Order by Column_Name) as No,Column_Name ,case Data_Type when 'datetime' then Data_Type when 'bit' then Data_Type when 'int' then Data_Type when 'text' then Data_Type when 'decimal' then Data_Type+'('+rtrim(cast(NUMERIC_PRECISION as char(5)))+','+rtrim(cast(NUMERIC_SCALE as char(5)))+')' when 'time' then Data_Type+'('+rtrim(cast(SCALE as char(5)))+')' when 'numeric' then Data_Type+'('+rtrim(cast(NUMERIC_PRECISION as char(5)))+','+rtrim(cast(NUMERIC_SCALE as char(5)))+')' else Data_Type+'('+cast(Lengths as varchar(5))+')' end as Data_Type ,cast(Descs as nvarchar(100)) as Descs From
(
Select name as Column_Name /*,iif(is_nullable = 0 , 'F','T') as is_nullable*/
,(Select DATA_TYPE From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = @tblName And COLUMN_NAME = sc.name) as Data_Type
,(Select CHARACTER_MAXIMUM_LENGTH From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = @tblName And COLUMN_NAME = sc.name) as Lengths
,(Select NUMERIC_PRECISION From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = @tblName And COLUMN_NAME = sc.name) as NUMERIC_PRECISION
,(Select NUMERIC_SCALE From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = @tblName And COLUMN_NAME = sc.name) as NUMERIC_SCALE
,(Select SCALE From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = @tblName And COLUMN_NAME = sc.name) as SCALE
,(Select value From sys.extended_properties Where major_id = sc.object_id and minor_id = sc.column_id) as Descs
From sys.columns sc Where object_id = (Select object_id From sys.tables Where name = @tblName)
) A Order by Column_Name
Post by Goborijung at 2022-07-27 11:11:26 | ID: 1727
--Insert Into TM_PO(PONo,PODate,POType,VendCode,VendName,ItemCode,Description,Unit,UnitCost,CostAmount,ExchangeRate,POQty,Branch,Department,RecQty,AdditionalQty,NAVLineNo)
--SELECT a.No_
--,a.[Order Date]
--,'0' as POType
--,a.[Buy-FROM Vendor No_]
--,a.[Pay-to Name]
--,b.No_ as ItemCode
--,b.[Description]
--,b.[Unit of Measure]
--,cast(round(b.[Unit Cost],4) as decimal(18,4)) as [Unit Cost]
--,cast(round(b.[Line Amount],2) as decimal(18,2)) as [Line Amount]
--,cast(round(a.[Currency Factor],2) as decimal(18,2)) as [Currency Factor]
--,cast(round(b.Quantity,2) as decimal(18,2)) as Quantity
--,1 as Branch
--,9 as Department
--,'0' AS RecQty
--,'0' as AdditQty
--,b.[Line No_]
--FROM [192.168.1.193].ThaiParfun_golive.dbo.[Thai Parfun Co_,Ltd$Purchase Header] a inner join [192.168.1.193].ThaiParfun_golive.dbo.[Thai Parfun Co_,Ltd$Purchase Line] b On a.No_ = b.[Document No_]
--WHERE Type = 2 AND b.No_ <> '' And [Document No_] in('PSO21120194')
--And b.No_ in('PKDT-M04215','PKDT-M04216','PKDT-M04217','PKDT-M04218','PKDT-M04219','PKDT-C86140','PKDT-C86141','PKDT-C86142','PKDT-C86143','PKDT-C86144','PKDT-C86147','PKDT-C86148','PKDT-C86149','PKDT-C86150','PKDT-C86151')
Post by Goborijung at 2021-04-22 16:10:59 | ID: 1137
ตัวอย่าง
Select cast(round(150.444444444444,4) as decimal(18,4)) as cutDigit -- 150.4444 หรือ Select CAST('150.4444444' AS DECIMAL(15, 2)) --150.44
Post by Goborijung at 2020-07-08 11:07:22 | ID: 660
Tools > Option... > Designers > เอาเครื่องหมายถูกหน้า Prevent saving changes that require ... ออก
Post by Goborijung at 2021-03-30 16:56:13 | ID: 346
https://stackoverflow.com/questions/39610133/how-to-import-an-excel-file-into-sql-server
Post by Goborijung at 2021-01-09 09:11:42 | ID: 289
SUBSTRING(expr,start,length) เลือกตัวอักษรใน expr โดยเริ่มจากค่าที่กำหนดใน start
Ex.
SELECT SUBSTRING('1234567',3,4) //Output : 3456Post by Goborijung at 2020-02-04 16:26:22 | ID: 396
DECLARE @myDate date=GETDATE();
SELECT @myDate as Today; -- หา วันที่ปัจจุบัน
/* รูปแบบ */
SELECT EOMONTH('วันเดือนปีปัจจุบัน' , -1 หรือ 1) as [End of last Month];
/* ตัวอย่าง */
SELECT EOMONTH(@myDate,-1) as [End of last Month]; -- หาวันที่สุดท้ายของ เดือนที่แล้ว
SELECT EOMONTH(@myDate) as [End of this Month]; -- หาวันที่สุดท้ายของ เดือนปัจจุบัน
SELECT EOMONTH(@myDate,1) as [End of next Month]; -- หาวันที่สุดท้ายของ เดือนถัดไปPost by Goborijung at 2020-08-22 15:28:28 | ID: 398
credit: https://bit.ly/2Ox9CT4 SQL UNION ALL เป็นคำสั่งที่ใช้สำหรับการเลือกข้อมูลโดยทำการรวมจำนวนแถวระหว่าง Table เข้าด้วยกัน สำหรับ UNION และ UNION ALL ต่างกันตรงที่ UNION จะเลือกข้อมูลที่ระหว่าง 2 ตาราง ตามกฏ DISTINCT ของ Table นั้น ๆ คือข้อมูลใน Table หนึ่ง ๆ จะไม่ซ่ำกัน UNION ALL เลือกข้อมูลโดยไม่สนใจ คือเลือกเอาทั้งหมด