0

I am creating a stored procedure which contains two while loops consequently, the below mentioned is used for creating few temp tables and the next loop writes some data to it.

The temp table name is retrieved from a static table

Issue: the below while loop after the very first iteration and goes to the next loop(second one). Am I doing something wrong here?

Or is this how while loops works in SQL Server (Azure SQL MI) within a procedure?

Also is it a good practice to create temp tables in this way?

declare @b varchar(100)
declare @bmax varchar(100)
declare @inpt_tbl_nm varchar(100)
declare @inpt_tbl_sql varchar(1000)
set @b = 1

select @bmax = max(RwId) from eds_int.abc

while @b < = @bmax
begin
    select @inpt_tbl_nm = otpt_tmp_tbl from eds_int.abc where Rwid=@b
    select @inpt_tbl_sql = 'create table ' + @inpt_tbl_nm + '(RowId INT IDENTITY(1,1), SchemaName sysname, TableName nvarchar(150), query nVARCHAR(4000), condition  nVARCHAR(4000))'

    exec (@inpt_tbl_sql)
    set @b = @b + 1
end

DECLARE @a INT
DECLARE @amax INT
DECLARE @max INT

SELECT @a = 1, @amax = MAX(RwId) from eds_int.abc

WHILE @a <= @amax
BEGIN
.
.
.
.
END

Edit:

declare @b int
declare @bmax int
declare @inpt_tbl_nm varchar(100)
declare @inpt_tbl_sql varchar(1000)
declare @test_query varchar(1000)

set @b = 1
select @bmax = 1
while @b < = @bmax
begin
print @b
select @inpt_tbl_nm = '#test_delete'
select @inpt_tbl_sql = 'create table ' + @inpt_tbl_nm + '(RowId INT IDENTITY(1,1), SchemaName sysname, TableName nvarchar(150), query nVARCHAR(4000), condition  nVARCHAR(4000))'
exec (@inpt_tbl_sql)
print @inpt_tbl_sql
select @test_query = 'select * from ' + @inpt_tbl_nm
exec (@test_query)
set @b = @b + 1
end

Error: Msg 208, Level 16, State 0, Line 1 Invalid object name '#test_delete'.

Venkat
  • 47
  • 7
  • 1
    The fact that you want to use `WHILE` loops at all is a major red flag, they perform awfully in any RDBMS. SQL isn't a programming language, it's a Query Language; use set based methods. What is your *real* goal here? – Thom A Dec 26 '19 at 15:55
  • @Larnu Thanks for your insights. The end goal here is to query few tables and store it within temp tables which further is joined and the resultant data is written to a static table. I understand that while loop adds up to the load but the case here is not much of a consuming scenario. Can you help me with the above query on where I am going wrong? – Venkat Dec 26 '19 at 16:11
  • 1
    That doesn't explain why you need a `WHILE` . – Thom A Dec 26 '19 at 16:14
  • Wait, are you going to be storing queries in your created tables (it has columns called `query` and `condition`)? That's a *really* bad idea... Extremely bad. You already suffer from SQL injection in the above, but storing queries in your tables is a very big indicator that your have some huge design flaws. – Thom A Dec 26 '19 at 17:11

1 Answers1

3

My points about the WHILE still stand from the comments; and the process is very likely flawed there as well. What you have though makes little sense.

You have the 2 variables @b and @bmax which are both varchar(100)s. I don't know what the value of @bmax is set to, but let's assume it's '10' (not 10) and you set the value of @b to '1' (not 1). Also The fact that you are using a varchar(100) implies you need to store a "number" up to (10*10^99)-1. Which is 1 short of a what is apparently called a "Googol".

You then enter the first loop, so the RDBMS checks is '1' is less than '10' (not 1 is less than 10). It is so the WHILE is entered. And the first iteration is performed.

At the end of that iteration you have set @b = @b + 1, which translate to the following (using Data Type Precedence):

@b + 1 = '1' + 1 = 1 + 1 = 2 ∴ @b = '2'

Then your WHILE starts again, and checks is '2' less than '10' (not is 2 less than 10); this fails as '2' is greater than '10', and so your WHILE is exited.

TL;DR: Data types are important. Strings aren't numbers and they do not behave like numbers.

Thom A
  • 88,727
  • 11
  • 45
  • 75
  • My mistake was with the way I have defined the data types, corrected them and it and that answers the question, I am good. Reg WHILE, I am completely new to these concepts and exploring on them. Will try to achieve the requirement in a different way and update. Additional question: What is the scope of temp tables, in the above case wont i be able to use these tables created dynamically?(Refer Edit) – Venkat Dec 27 '19 at 09:18
  • I don't see any temporary tables on your SQL @Venkat. But, a temporary tables only persists for the duration of the scope and connection that created them. – Thom A Dec 27 '19 at 10:01