Find a field name in some tables

xiaoxiao2021-03-06  87

Recently, you have to modify the bulk value of some fields, such as gender code and ID conversion, so write a function to call. It's very simple to laugh.

Create Function F_GettablenameList (@incolumnname as varchar (50))

Returns varchar (500)

Begin

- internal variable

Declare @Column Varchar (50), @ Table Varchar (50), @ STEMP VARCHAR (500)

- Declare a cursor

Set @ STEMP = ''

Declare mycursor cursor for

Select distinct a.name as column_name, b.name as table_name from syscolumns a, sysobjects b where A.Id = B.ID and b.XTYPE = 'u' and

A.Name='' @incolumnname ''

- Open a cursor

Open mycursor

--Extract data

Fetch next from mycursor

INTO @ Column, @ Table

- If the extracted data is successful

While @@ fetch_status = 0

Begin

Set @Stemp = @Stemp @Table ','

Fetch next from mycursor

INTO @ Column, @ Table

end

- Remove extra more than

IF (Len (@STEMP)> 1)

Begin

Set @ STEMP = Substring (@ Stemp, 1, Len (@STEMP) -1)

end

--Print @stemp

- Close the cursor, release the cursor

Close mycursor

Deallocate mycursor

Return (@STEMP)

end

转载请注明原文地址:https://www.9cbs.com/read-96216.html

New Post(0)