需要使用SQL Server中的存储过程获取ID

时间:2013-10-16 09:06:57

标签: sql sql-server stored-procedures

CREATE TABLE myTable99 (TABLE_NAME sysname, COLUMN_NAME sysname, Occurs int)
GO

SET NOCOUNT ON

DECLARE @SQL varchar(8000),
@TABLE_NAME sysname, 
@COLUMN_NAME sysname,
@Sargable varchar(80), 
@Count int

SELECT @Sargable = 'PS'

DECLARE insaneCursor CURSOR FOR 
SELECT c.TABLE_NAME, c.COLUMN_NAME 
  FROM INFORMATION_SCHEMA.Columns c INNER JOIN INFORMATION_SCHEMA.Tables t
    ON t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME
 WHERE c.DATA_TYPE IN ('char','nchar','varchar','nvarchar','text','ntext')
   AND t.TABLE_TYPE = 'BASE TABLE'

OPEN insaneCursor

FETCH NEXT FROM insaneCursor INTO @TABLE_NAME, @COLUMN_NAME

WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @SQL = 'INSERT INTO myTable99 (TABLE_NAME, COLUMN_NAME, Occurs) SELECT '
            + '''' +  @TABLE_NAME + '''' + ','
            + '''' + @COLUMN_NAME + '''' + ','
            + 'COUNT(*) FROM [' + @TABLE_NAME  
            + '] WHERE [' + @COLUMN_NAME + '] Like '
            + ''''+ '%' + @Sargable + '%' + ''''
    --SELECT @SQL
    EXEC(@SQL)
    IF @@ERROR <> 0 
    ------ <> means Not Equal To
        BEGIN 
            SELECT @SQL
            SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = @TABLE_NAME
            GOTO Error
        END 
    FETCH NEXT FROM insaneCursor INTO @TABLE_NAME, @COLUMN_NAME
END

SELECT * FROM myTable99 WHERE Occurs <> 0 

Error:
CLOSE insaneCursor
DEALLOCATE insaneCursor

GO

DROP TABLE myTable99
GO

SET NOCOUNT OFF

以下存储过程将搜索字符串'PS'并返回COLUMN_NAME和TABLE_NAME,但我想要相应列中PS的ID值

例如:

    ID      NAME
     2      PW
     3      PS

现在存储过程只返回COLUMN_NAME,TABLE_NAME,但当我搜索字符串'PS'以及COLUMN_NAME和TABLE_NAME时,我需要3的ID

我试过

 SELECT @SQL = 'INSERT INTO myTable99 (TABLE_NAME, COLUMN_NAME, Occurs) SELECT *'
             + 'FROM [' + @TABLE_NAME  
             + '] WHERE [' + @COLUMN_NAME + '] Like '
             + ''''+ '%' + @Sargable + '%' + ''''                                                               

但是,即使在表

中找不到该字符串,存储过程也会返回数据库中的所有表和列名

myTable99

    TABLE_NAME  COLUMN_NAME  Occurs
     project     projectname  2
     task        name         1

现在我需要一个名为ID的新列,它在表项目中获取项目名称'PS'的ID

预期结果:

假设表项目有PS条目

表名:项目

专栏:

          ID  projectname 
           2    PR
           3    PS

现在存储过程将返回Table_Name=Projectcolumn_Name=projectname但不返回Id,我需要字符串PS的ID为3

结果:当我搜索string ='PS'

 ID   TABLE_NAME  COLUMN_NAME  Occurs
 3    Project     projectname   1

1 个答案:

答案 0 :(得分:0)

您的问题不是很明确,我认为您需要将projectId与myTable99中的其他列一起使用。你可以这样做。

--++++++++++++++++++++++++++++++++++++++++++++++++
--Before/after your cursor (not within the cursor)
DECLARE @ProjectId INT 

--Assuming projectName as a unique constraint
SELECT @ProjectId = ProjectId
FROM yourProjectTable
WHERE projectName = @Sargable

--++++++++++++++++++++++++++++++++++++++++++++++++    

--At the end, replace your select query with
SELECT TABLE_NAME, COLUMN_NAME, Occurs, @ProjectId ProjectId
FROM myTable99 
WHERE Occurs <> 0