[SQL] Các câu truy vấn SQL nên học

 1) Copy một column trong một table  

INSERT INTO [System].[dbo].[Website]( [Id] ,[Code]

      ,[Name]
      ,[Status]
      ,[CompanyId]
      ,[DepartmentId)
SELECT '1', '1', 'Special', '1',[CompanyId]
      ,[DepartmentId
FROM [System].[dbo].[Website] WHERE [Id] = '000005'
 
 
2) INSERT INTO Toàn bộ dữ liệu từ một bảng khác, DB khác
INSERT INTO [Comment].[dbo].[ObjectInfo] (
      [Id]
      ,[objectid]
      ,[Title]
      ,[CreatedDate]
      ,[websiteId]
      ,[CreatedDateUtc]
      ,[CreatedUid], [UpdatedDate],[UpdatedDateUtc], [UpdatedUid],[LoginUid],[Description],[LanguageId],[Type])
SELECT DISTINCT '000003_1_' + [commentbk].[dbo].[comment].ObjectId AS 'Id', [commentbk].[dbo].[comment].ObjectId AS 'objectId', '','', '' ,'','','','','','','','','' FROM [commentbk].[dbo].[comment] 
 
3) UPDATE dữ liệu từ một bảng, DB khác
UPDATE [Comment].[dbo].[ObjectInfo]
SET [Comment].[dbo].[ObjectInfo].Title = B.Title
FROM [Comment].[dbo].[ObjectInfo] A, [commentbk].[dbo].[comment] B
WHERE A.ObjectId = B.objectid

Comments

Popular posts from this blog

31 Quy tắc trong thiết kế Cơ sở dữ liệu