T-SQL Extractor

Source: Internet
Author: User
Tags comments continue

/******************************************************************************
* Author:iret
* Desc:t-sql Extractor
* Extract the comments and blanks and tabs from the SQL statement
* To compare two stored procedures, or if the SQL statements are consistent, take the time to write a script that can delete comments and spaces in T-SQL statements, which is fine.
* Created DATE:2004/10/21
******************************************************************************/

DECLARE @script VARCHAR (8000), @extractedScript VARCHAR (4000)
SET @script = '
/* script to get stored procedure from system table * *
SELECT @script = @script + [text]
From syscomments, sysobjects
WHERE
syscomments. [id] = sysobjects. [id] and sysobjects. [Name] Like '%campa_appsegment '

/* Symbol * *
DECLARE @InLineCommented bit, @InSectionCommented bit, @InString bit, @position INT

/* Current Character/*
DECLARE @curChar INT

/* Previous Character * *
DECLARE @preChar INT

SET @InLineCommented = 0
SET @InSectionCommented = 0
SET @InString = 0

SET @extractedScript = '
SET @position = 1
SET @preChar = null

While @position <= datalength (@script)
BEGIN
--Get current character
SET @curChar = ASCII (SUBSTRING (@script, @position, 1))
IF @preChar = ASCII ('/') and @curChar = ASCII (' * ') and @InLineCommented = 0 and @InString = 0
BEGIN
--SET The sign in section comment
SET @InSectionCommented = 1

--pop The/char
SET @extractedScript = substring (@extractedScript, 1,len (@extractedScript)-1)

SET @preChar = @curChar
SET @position = @position + 1
CONTINUE
End

IF @preChar = ASCII (' * ') and @curChar = ASCII ('/') and @InLineCommented = 0 and @InString = 0
BEGIN
SET @InSectionCommented = 0
SET @preChar = @curChar
SET @position = @position + 1
CONTINUE
End

IF @preChar = ASCII ('-') and @curChar = ASCII ('-') and @InSectionCommented = 0 and @InString = 0
BEGIN
SET @InLineCommented = 1

--pop The/char
SET @extractedScript = substring (@extractedScript, 1,len (@extractedScript)-1)

SET @preChar = @curChar
SET @position = @position + 1
CONTINUE
End

IF @curChar = ASCII (' ") and @InString = 0 and @InSectionCommented = 0 and @InLineCommented = 0
BEGIN
SET @InString = 1
End

IF @inString = 1 and @curChar = ASCII (' ")
BEGIN
IF ASCII (SUBSTRING (@script, @position +1, 1)) = ASCII (' ")
BEGIN
SET @extractedScript = @extractedScript + ' "'
SET @position = @position + 1
End
ELSE
BEGIN
SET @InString = 0
End
End

IF @InSectionCommented = 1
BEGIN
SET @preChar = @curChar
SET @position = @position + 1
CONTINUE
End

IF @InLineCommented = 1
BEGIN
--If meets the ' end ' of the ' line set ' inlinecommented to False
IF @curChar = ten and @preChar = 13
BEGIN
SET @InLineCommented = 0
End

SET @preChar = @curChar
SET @position = @position + 1
CONTINUE
End

IF @curChar = ASCII (') or @curChar = ten or @curChar =13 or @curChar = ASCII (') or @curChar = 32
BEGIN
SET @preChar = @curChar
SET @position = @position + 1
CONTINUE
End

SET @extractedScript = @extractedScript + CHAR (@curChar)
SET @preChar = @curChar
SET @position = @position + 1
End

--Print the result script
SELECT @extractedScript








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.