T-SQL: Looping through an array of known values

后端 未结 7 1657
深忆病人
深忆病人 2021-01-30 00:51

Here\'s my scenario:

Let\'s say I have a stored procedure in which I need to call another stored procedure on a set of specific ids; is there a way to do this?

i

7条回答
  •  半阙折子戏
    2021-01-30 01:04

    Make a connection to your DB using a procedural programming language (here Python), and do the loop there. This way you can do complicated loops as well.

    # make a connection to your db
    import pyodbc
    conn = pyodbc.connect('''
                            Driver={ODBC Driver 13 for SQL Server};
                            Server=serverName;
                            Database=DBname;
                            UID=userName;
                            PWD=password;
                          ''')
    cursor = conn.cursor()
    
    # run sql code
    for id in [4, 7, 12, 22, 19]:
      cursor.execute('''
        exec p_MyInnerProcedure {}
      '''.format(id))
    

提交回复
热议问题