Procedure for obtaining the required column name based on the table name and Index

Source: Internet
Author: User

Copy codeThe Code is as follows:
Create proc p_sword_getblcolumn
(
@ TblName varchar (200 ),
@ FromIndex int,
@ ToIndex int,
@ Column name varchar (3000) output
)
As
Begin
Declare @ tempColumn varchar (3000)
Declare @ errMsg varchar (200)
Declare @ I int
Set @ I = 1
Set @ columnName =''
Set @ errMsg =''
Declare tempColumnCur cursor
Select syscolumns. name from syscolumns join sysobjects on syscolumns. id = sysobjects. id
Where sysobjects. name = @ tblName order by syscolumns. colorder
Open tempColumnCur
Fetch next from tempColumnCur into @ tempColumn
While @ FETCH_STATUS = 0
Begin
If (@ fromIndex = 0 and @ toIndex = 0)
Begin
Set @ columnName = @ columnName + ',' + @ tempColumn
End
If (@ fromIndex = 0 and @ toIndex <> 0)
Begin
If (@ I <= @ toIndex)
Set @ columnName = @ columnName + ',' + @ tempColumn
End
Else if (@ fromIndex <> 0 and @ toIndex = 0)
Begin
If (@ I >=@ fromIndex)
Set @ columnName = @ columnName + ',' + @ tempColumn
End
Else if (@ I >=@ fromIndex and @ I <= @ toIndex)
Begin
Set @ columnName = @ columnName + ',' + @ tempColumn
End
Set @ I = @ I + 1
Print @ I
Fetch next from tempColumnCur into @ tempColumn
End
Close tempColumnCur
Deallocate tempColumnCur
Set @ columnName = SUBSTRING (@ columnName, 2, len (@ columnName ))
Print @ columnName
If (@ ERROR <> 0)
Begin
Set @ errMsg = 'get column error'
Goto errorproc
End
Else
Return 0
End
Errorproc:
Begin
Raiserror (@ errMsg, 16, 1)
Return 1
End
Go

The raiserror function is involved.
RAISERROR ({msg_id | msg_str | @ local_variable}
{, Severity, state}
[, Argument [,... n]
)
[WITH option [,... n]
Parameter description:
First parameter: {msg_id | msg_str | @ local_variable}
Msg_id: indicates the message code defined in the sys. messages table;
Use sp_addmessage to store the User-Defined error message number in the sys. messages directory view.
The error code of the custom error message must be greater than 50000.
Msg_str: indicates a user-defined message. The error message can contain a maximum of 2047 characters;
(If it is a constant, use n'xxx' because it is nvarchar)
When msg_str is specified, RAISERROR will trigger an error message with the error code 5000.
@ Local_variable: format the string variable in msg_str mode.
Second parameter: severity
The severity level of the message associated with the user-defined message. (This is important)
Any user can specify a severity level between 0 and 18.
Within the closed interval of [], catch is not skipped;
If it is [11,19], it will jump to catch;
If [20, infinite), the database connection is terminated directly;
Third parameter: state
If the same user-defined error is thrown at multiple locations,
You can use a unique status number for each location to locate the code segment that causes the error.
Any integer between 1 and 127. (The default value of state is 1)
An error is generated when the state value is 0 or greater than 127!
Fourth parameter: argument
The parameter used to replace msg_str or the variable defined in the message corresponding to msg_id.
Fifth parameter: option
Incorrect custom options can be any of the following values:
LOG: records errors in error logs and application logs;
NOWAIT: send the message to the client immediately;
SETERROR: set the value of @ ERROR and ERROR_NUMBER to msg_id or 50000;

Related Article

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.