需要在所有非系统数据库上运行存储过程

时间:2019-05-29 21:08:39

标签: sql-server tsql sql-server-2012

我试图遍历所有非系统数据库并运行存储过程。此存储过程存在于所有用户数据库中。

这是我到目前为止发现的:

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<div id="root"></div>

<script type="text/babel">
const colors = ["white", "green", "blue", "red", "orange"];

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      color: colors[0]
    };
  }

  setCurrentColor(setColor) {
    this.setState({
      color: setColor
    });
  }

  setColorSelections() {
    const colorItems = colors.map((thecolor) =>
      <li
        onClick={(event) => this.setCurrentColor(thecolor)}
        key={thecolor} >
        {thecolor}
      </li>
    );
    return (
      <ul>
        {colorItems}
      </ul>
    )
  }
  render() {
    return (
    <div> 
        <p style={{ backgroundColor: this.state.color }}>Current selection: {this.state.color}</p>
              {this.setColorSelections()}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
</script>

此错误,我得到一个错误:

  

第15级状态1行3的消息102
  '@command'附近的语法不正确。

     

第28行,州62,第16级,消息28
  找不到存储过程'Support.CleanIndiciesAndShrinkDatabase'。

关于解决此问题的任何建议?

3 个答案:

答案 0 :(得分:1)

尝试以下


DECLARE @command varchar(1000) 
DECLARE @spName VARCHAR(50)
SET @spName = 'Support.CleanIndiciesAndShrinkDatabase'

SELECT @command = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') 
BEGIN 
    USE ? 
        IF EXISTS(SELECT TOP 1 1 FROM sys.procedures AS P WHERE p.name = ''' + @spName + ''')
        BEGIN
            PRINT ''running '+ @spName + ' on '' + DB_NAME()
            EXEC ' +@spName+' 
        END
        ELSE
        BEGIN 
            PRINT ''' + @spName + ' was on found on database '' + DB_NAME()''
        END

END ' 

EXEC sp_MSforeachdb @command

它将在所有非系统数据库上运行。 现在,您收到的错误意味着SQL Server无法找到存储过程。您可以通过在尚不存在的任何数据库上创建存储过程然后运行它来解决此问题。 因此,更好的查询是


DECLARE @command varchar(1000) 
DECLARE @schemaName VARCHAR(50)
DECLARE @spName VARCHAR(50)
SET @schemaName  = 'Support'
SET @spName = 'CleanIndiciesAndShrinkDatabase'

SELECT @command = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') 
BEGIN 
    USE ? 

        IF NOT EXISTS(SELECT TOP 1 1 
                        FROM sys.procedures AS P 
                            INNER JOIN sys.schemas AS S ON S.schema_id = P.schema_id
                        WHERE p.name = ''' + @spName + '''
                            AND s.name = ''' + @schemaName + ''')
        BEGIN
            PRINT ''creating '+ @spName + ' on '' + DB_NAME()
            IF NOT EXISTS (   SELECT TOP 1 1
                  FROM   sys.schemas AS S
                  WHERE  S.name = ''' + @schemaName + ''' )
                BEGIN
                    PRINT ''CREATING SCHEMA ' + @schemaName + '''
                    EXEC ( '' CREATE SCHEMA ' + @schemaName + ''' );
                END;

                EXEC ( ''
                    CREATE PROCEDURE ' + @schemaName + '.' + @spName + '
                    AS
                    BEGIN  
                        -- SP CODE GOES HERE
                        -- SELECT COUNT(*) FROM SYS.TABLES --uncomment this for check
                    END 
                '' );
        END

        PRINT ''running '+ @spName + ' on '' + DB_NAME()
        EXEC ' + @schemaName + '.' + @spName +' 

END ' 

EXEC sp_MSforeachdb @command



答案 1 :(得分:0)

尝试这样的事情:

DECLARE @DynamicTSQLStatement NVARCHAR(MAX);

SELECT @DynamicTSQLStatement = STUFF
(
    (
        SELECT 'USE [' + [name] + ']; EXECUTE Support.CleanIndiciesAndShrinkDatabase;' 
        FROM [sys].[databases]
        WHERE [name] NOT IN ('master', 'tempdb', 'model', 'msdb')
        FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)')
    ,1
    ,1
    ,''
);

EXECUTE sp_executesql @DynamicTSQLStatement;

答案 2 :(得分:0)

您可以尝试以下方法:

Create PROC PROC_NAME 
AS
BEGIN
DECLARE @name nvarchar(50)
declare @cursor cursor
set @cursor = CURSOR FAST_FORWARD FOR select name from sys.databases where database_id > 4

open @cursor

FETCH NEXT FROM @cursor INTO @name 

WHILE @@FETCH_STATUS = 0

BEGIN

DECLARE @use nvarchar(50) = 'USE '

SET @use = @use + @name


Declare @query nvarchar(max) = @use + ' exec Your_PROC'


EXEC sp_executesql  @query

FETCH NEXT FROM @cursor INTO @name 

END

CLOSE @cursor

DEALLOCATE @cursor

END