I have this sql code and I need to format the query string in python. First, I want to…
- User
I have this sql code and I need to format the query string in python. First, I want to format the declare text block and nothing else. What I want is to align the variable types text, so they all start at the same position.
use db_name go declare @cursor_table_name cursor, @var1 tinyint , @var2 smallint , @var3 varchar(10) , @var4 int , @var5 varchar(64) , @var6 int , @var7 varchar(64) , @var8 varchar(1) declare cursor_table_name insensitive cursor for select var1 , var2 , ... var8 from db_name..table_name for read only open cursor_table_name fetch cursor_table_name into @var1 , @var2 , @var3 , @var4 , @var5 , @var6 , @var7 , @var8 while @@sqlstatus = 0 begin fetch cursor_table_name into @var1 , @var2 , @var3 , @var4 , @var5 , @var6 , @var7 , @var8 end close cursor_table_name deallocate cursor_table_name - Assistant
- User
---> 80 max_length = max(len(part.split()[0]) for part in parts) + 1 # Get max length of variable names ... ---> 80 max_length = max(len(part.split()[0]) for part in parts) + 1 # Get max length of variable names 82 for part in parts: 83 name_type = part.strip().split()
IndexError: list index out of range
- Assistant
- User
ok I want you to align them based on the max length + 1 of the longest variable name, so they all align vertically with the longest variable name
- Assistant
- User
ok the code works great but has some bugs:
-
Don't strip the start of the lines, they are already aligned or should be aligned two spaces. Maybe you can check if there are two spaces and if don't add them but don't remove them. Also this only happen inside the declare block, the rest of the string should not be checked.
-
More importantly, only affect the code inside the declare block, I see you removed some whitespaces elsewhere, don't do that.
-
- Assistant
- User
Well now the declare block is perfect, but I can see that there is a fetch block which was stripped from the spaces, and this is outside the declare block, this is wrong, why happened that?
Original: ... fetch cursor_some_table into @w_some_variable1 , @w_some_variable2 , @w_some_variable3 , @w_some_variable4 , ...
Now in the output of this code: ... fetch cursor_some_table into @w_some_variable1, @w_some_variable2, @w_some_variable3, @w_some_variable4, ...
So it stripped both the start and the end before the commas, which shouldn't be modified since this part is outside the declare block.
- Assistant
- User
it did the same thing as the previous code, and it is still modifying outside of the declare block. is there some debug printing that we can use so we can tell where is messing the code or maybe you know where is the problem already?
- Assistant