상용 sql 문장 정리
29324 단어 ql 문장
CREATE PROCEDURE [dbo].[bbs_move_createtopic]
@fid smallint,
@iconid smallint,
@curtid INT OUTPUT
AS
BEGIN
INSERT INTO [topics] ......
SET @topicid=SCOPE_IDENTITY()
END
declare @curtid int
exec [createtopic] @fid=@new_fid,@iconid=0,
@curtid=@curr_topicid OUTPUT
2. 커서
DECLARE
@name NVARCHAR(100),
@id DECIMAL,
@idcard NVARCHAR(100)=NULL
--
DECLARE cur_correctIdCard CURSOR
FOR
SELECT
ID, NAME, [USER_ID]
FROM dbo.old_bbstitle
WHERE (USER_ID IS NULL OR LTRIM(NAME)='')
OR (NAME IS NULL OR LTRIM(NAME)='')
--
OPEN cur_correctIdCard
-- FETCH
FETCH NEXT FROM cur_correctIdCard INTO @id,@name,@idcard
--0 FETCH
--1 FETCH
--2
WHILE(@@FETCH_STATUS=0)
BEGIN
... ...
FETCH NEXT FROM cur_correctIdCard INTO @id,@name,@idcard
SET @idcard=NULL
END
CLOSE cur_correctIdCard
DEALLOCATE cur_correctIdCard
3. 테이블 데이터 복사
SELECT * INTO users_bak FROM users
4. group by … having …
DELETE
FROM dbo.old_clubuser WHERE CLUBUSER_ID
IN(
SELECT MIN(CLUBUSER_ID) FROM dbo.old_clubuser WHERE CLUBUSER_NAME IS NOT NULL AND CLUBUSER_NICKNAME IS NOT NULL
GROUP BY CLUBUSER_NAME,CLUBUSER_NICKNAME
HAVING COUNT(CLUBUSER_NAME)>1
)
5. 트랜잭션
BEGIN TRANSACTION trans_correctIdCard
BEGIN TRY
DECLARE
@name NVARCHAR(100),
@id DECIMAL,
@idcard NVARCHAR(100)=NULL
COMMIT TRANSACTION trans_correctIdCard
PRINT 'SUCCESS'
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION trans_correctIdCard
PRINT ' :'+CAST(@@error AS NVARCHAR(MAX))
PRINT ' '
END CATCH
5. 표의 존재 여부를 판단하고 표를 만든다
-- ,
IF object_id(N'dbo.T_Student',N'U') IS NOT NULL
DROP TABLE dbo.T_Student
GO
--
CREATE TABLE [dbo].[T_Student](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[Age] [int] NOT NULL,
[NickName] [nvarchar](max) NULL,
[City] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.T_Student] PRIMARY KEY CLUSTERED ([Id] ASC)
)
6. SQL Server 테이블 구조 가져오기
SELECT (case when a.colorder=1 then d.name else null end) ,
a.colorder ,a.name ,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) ,
(case when (SELECT count(*) FROM sysobjects
WHERE (name in (SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
AND (xtype = 'PK'))>0 then '√' else '' end) ,b.name ,a.length ,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as ,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as ,(case when a.isnullable=1 then '√'else '' end) ,
isnull(e.text,'') ,isnull(g.[value], ' ') AS [ ]
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
left join sys.extended_properties f on d.id=f.class and f.minor_id=0
where b.name is not null
--WHERE d.name=' ' -- ,
order by a.id,a.colorder
(출처:http://www.cnblogs.com/ynbt/archive/2012/07/16/2593389.html)
7. 데이터베이스에 있는 모든 데이터베이스 이름 조회
SELECT Name FROM Master..SysDatabases ORDER BY Name
8. 어떤 데이터베이스에 있는 모든 테이블 이름을 조회
SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
9. 라이브러리의 테이블을 대량으로 비운다(1)
SELECT 'TRUNCATE TABLE ' + Name FROM SysObjects Where XType='U' ORDER BY Name
10. 라이브러리의 테이블을 대량으로 비우기(2)
USE [ ]
DECLARE
@tableName NVARCHAR(100)
--
DECLARE CUR_ALLTABLES CURSOR
FOR
SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
--
OPEN CUR_ALLTABLES
-- FETCH
FETCH NEXT FROM CUR_ALLTABLES INTO @tableName
DECLARE @SQL VARCHAR(MAX) = ''
--0 FETCH
--1 FETCH
--2
WHILE(@@FETCH_STATUS=0)
BEGIN
SET @SQL = 'TRUNCATE TABLE ' + @tableName
PRINT @SQL
FETCH NEXT FROM CUR_ALLTABLES INTO @tableName
END
CLOSE CUR_ALLTABLES
DEALLOCATE CUR_ALLTABLES
11. 행렬 전환의 예
WITH CTE AS (
select 1 as ,'2010-2-3' as ,' ' as
union all
select 2 as ,'2010-2-3' as ,' ' as
union all
select 3 as ,'2010-2-4' as ,' ' as
union all
select 4 as ,'2010-2-4' as ,' ' as
union all
select 5 as ,'2010-2-3' as ,' ' as
union all
select 6 as ,'2010-2-3' as ,' ' as
union all
select 7 as ,'2010-2-4' as ,' ' as
)
SELECT , SUM( ) , SUM( ) FROM (
SELECT ,
CASE WHEN =' ' THEN 1 ELSE 0 END AS ,
CASE WHEN =' ' THEN 0 ELSE 1 END AS FROM CTE
)A GROUP BY
12.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
유용한 SQL 문(중복 레코드 삭제, 로그 축소)텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.