简体   繁体   中英

How do I get my for loop to replace these words in a T-SQL query in python?

I'm still new to Python and am learning new things everyday, but I keep running into issues that I'm not finding answers for online, despite being simple problems.

I'm simply trying to run through a loop so I don't have to repeat the same multiple queries with just one different word for each one.

I've tried setting a variable named

assets = ["AT", "AST", "PAC", "AC"]

I tried using

for i in range(4)
for i in assets

and a few more examples, but i keep getting the error Invalid column name 'i'

Which is obvious why, because it's not going through the array or vector like i would like it to.

assets = ["AT", "AST", "PAC", "AC"]
i = 0
for i in assets:

    q1 = """\
SELECT 'UnC' AS [i], SUM(CASE WHEN [i] IS NULL THEN 1 ELSE 0 END) AS [i 
Count]
    FROM SM.dbo.vSC SCL
        JOIN IP.dbo.vSLUN(3) S
            ON SCL.iSID=S.isID
        JOIN SMa.dbo.S_C SC(noLock) ON S.isID=SC.iSID
WHERE SC.bIsInA IS NULL AND [i] IS NULL"""

df1 = pd.read_sql(q1, conn)
print(df1)

I would like to achieve running 4 different queries with the names changing after each query is finished being stored in a variable, which I would eventually like to print out the results with, for each query. Any help would be appreciated, thank you.

Write your query for example like this:

q1 = """\

  SELECT *REPLACETHIS* FROM whatever;

Then say

for i in assets:
  print(q1.replace("*REPLACETHIS*", i))

Hope this helps

In Python, you cannot interpolate strings in the manner you attempted, specifically embedding a value directly in another string body which may be possible in other langauges (PHP, Perl, etc.).

Usually in Python, placeholders and binded values are used. While there are multiple ways to interpolate strings in Python (>=2.6), the preferred universal way is with str.format . Since you need to format the same value, place all with first ordinal position, {0} :

for i in assets: 
    q1 = """SELECT 'UnC' AS [{0}], SUM(CASE WHEN [{0}] IS NULL THEN 1 ELSE 0 END) AS [{0} Count]
            FROM SM.dbo.vSC SCL
            JOIN IP.dbo.vSLUN(3) S
                ON SCL.iSID = S.isID
            JOIN SMa.dbo.S_C SC(noLock) ON S.isID = SC.iSID
            WHERE SC.bIsInA IS NULL AND [{0}] IS NULL
         """

    q1 = q1.format(i)

    df1 = pd.read_sql(q1, conn)
assets = ["AT", "AST", "PAC", "AC"]

x = 0
for x in assets[0:4]:

    q1 = """\
SELECT 'Unc' AS [x], SUM(CASE WHEN [x] IS NULL THEN 1 ELSE 0 END) AS [x 
Count]
    FROM SM.dbo.vSC SCL
        JOIN IP.dbo.vSLUNode(3) S
            ON SCL.iSID=S.isID
        JOIN SM.dbo.S_C SC ON 
S.isID=SC.iSID
WHERE SC.bIIA IS NULL AND [x] IS NULL"""


print(q1.replace("x", x))






 assets = ["AT", "AST", "PAC", "AC"]


for i in assets[0:4]:

    q1 = """\
SELECT 'Unc' AS [{0}], SUM(CASE WHEN [{0}] IS NULL THEN 1 ELSE 0 END) AS [{0} Count]
    FROM SM.dbo.vSC SCL
        JOIN IP.dbo.vSLUNode(3) S
            ON SCL.iSID=S.isID
        JOIN SM.dbo.S_C SC ON 
S.isID=SC.iSID
WHERE SC.bIIA IS NULL AND [{0}] IS NULL"""


q1 = q1.format(i)
df1 = pd.read_sql(q1, conn)
print(df1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM