How to loop through an array of integers in SQL

sql loop through array of strings
array in sql
sql loop through select results
sql for loop
sql string to array
sql declare array of strings
sql loop through table
sql server loop through table and update

I have 3 SQL tables:

  • The first one: contains Codes [1 .. 22] and each code represents a value. This list is always fixed (no changes on it) - I am going to call the column Code1
  • The second one contains also contains Codes, and each time we use a set of codes and each code represents a value - I am going to call the column Code2
  • The third table has 2 columns (2 FKs) Code1|Code2 coming from the previous tables

My question is I want to iterate always through all of the values of Code1 column from the first table (no matter how many codes I have) and for a certain/specific values of Code2 column from the second table, let's say [30.. 55]. I want to insert the Code1 and Code2 into the third table.

Example:

Table 1
Code1|Descript
1|A
2|B
3|C

Table 2
Code2|Descript
1|AA
2|BB
3|CC
4|DD
5|EE

I want to insert into my third table the following values

Table 3
Code1|Code2
1|2
1|3
1|4
2|2
2|3
2|4
3|2
3|3
3|4

Basically looping through every value from Table 1 and for specific values from Table 2 (in my example [2,3, 4]), I made the insert queries

What is the optimal way to do those insert queries without having to create an insert query for every single entry?

My solution is something like the following:

INSERT INTO T3 
   SELECT T1.Code1, T2.Code2
   FROM T1 CROSS JOIN (
       SELECT Code2
       FROM T2
       WHERE Code2 BETWEEN x and y)T2

Thanks,

INSERT INTO TABLE3 (code1, code2) 
     SELECT 
           T1.Code1, T2.Code2 
               FROM Table1 T1 JOIN Table2 T2
               ON T2.code2 IN(2,3,4)

Iterate Through Array Of Data In SQL Query, Solution. Use DATABASENAME. GO. DECLARE @PRODUCTDETAILSTABLE table (PRODUCTNAME nvarchar(100), PRODUCTID int, PRODUCTCOST int) -- Declare your array table variable. DECLARE @MYARRAY table (TEMPCOL nvarchar(50), ARRAYINDEX int identity(1,1) ) Loop Through Integer Array. Ask Question Asked 6 years, 4 months ago. Active 6 years, 4 months ago. Viewed 3k times 0. What I am trying to do is loop through an

An INSERT statement where we cross join the two tables (Selecting the range in the second table you are after) should do the trick here:

INSERT INTO table3
SELECT table1.code1, table2.code2
FROM table1, table2
WHERE table2.code2 BETWEEN 2 and 4

If you don't want duplicates hitting table3 should the relationship already exist then you can do something like:

INSERT INTO table3
SELECT table1.code1, table2.code2
FROM table1, table2
WHERE table2.code2 BETWEEN 2 and 4
   AND (table1.code1, table2.code2) NOT IN (SELECT code1, code2 FROM table3)

How to implement array-like functionality in SQL Server, How to use a table variable instead of an array; The function DECLARE @​myTableVariable TABLE (id INT, name varchar(20)). insert into We do not use loops (WHILE) in general because it is slower and it is not efficient. T-SQL: Looping through an array of known values. Ask Question Asked 10 years, 8 months ago. Active 5 months ago. Then loop based on the rows in this table.

I think query needs to be cross join as you need all rows from first table paired with certain rows in second table

INSERT INTO TABLE3 (code1, code2) 
     SELECT 
           T1.Code1, T2.Code2 
               FROM Table1 T1 CROSS JOIN Table2 T2
               ON T2.code2 IN(2,3,4)

for loop list of Array in stored procedure, Iterate through month 'array' (that is, table) declare @m int = 1 while @m <= 12 begin select MonthName from @months where MonthNum = @  How to loop through an array of integers in SQL. Ask Question Asked 1 month ago. Active 1 month ago. Viewed 53 times 0. I have 3 SQL tables: The first one: contains

Looping through Array Values in T-SQL, DECLARE @RecipientID INT DECLARE @ArrayValues VARCHAR (255) How can I loop through each value in ArraySplit and compare that  Generating arrays of integers. GENERATE_ARRAY generates an array of values from a starting and ending value and a step value. For example, the following query generates an array that contains all of the odd integers from 11 to 33, inclusive:

Looping through table records in Sql Server, (Id INT , Name NVARCHAR(100), Status TINYINT). GO Approach 1: Looping through table records with static loop counter initialization ? Iterating over collection variables [PL/SQL] These are cook book recipes that show, for each of the three collection types , how to iterate over their elements. Nested tables

Example of using arrays in an SQL procedure, An example demonstrates many of the ways that you can use arrays in a native SQL built-in function to determine how many times to execute a WHILE loop. INTCOL INT)# -- SQL PROCEDURE PROCESSPERSONS HAS THREE ARRAY​  PL/SQL FOR LOOP examples. Let’s take some examples of using the FOR LOOP statement to understand how it works. A) Simple PL/SQL FOR LOOP example. In this example, the loop index is l_counter, lower_bound is one, and upper_bound is five. The loop shows a list of integers from 1 to 5.

Comments
  • First, SQL Server has no concept of "arrays", so I don't understand the question. Second, you seem to be suggesting that you are storing multiple values in a string column. That just means that you have a broken data model. Fix it.
  • Do a JOIN, but I need to see a better description of the data to say what that join looks like.
  • That's true Gordon.. The way I put my question relates more to my programming background. The data base is fine and normalized
  • Please share some sample data from these three tables and your desired results after this process runs. Your question is confusing since you are using terminology that doesn't fit well with rdbms. I agree with @Riad that this is a simple INSERT using a join and WHERE clause, but some sample and desired results would help a ton.
  • I will edit my question. Thank you all
  • Thank you so much @Cato for the answer, the solution that I wrote does not have join but this seems to be better.
  • Thank you @JNevill for your answer. Yes, I had the same solution but I missed the idea behind your second query. This helped me understand it better
  • Indeed, Thank you Dee P
  • do you use 'ON' with 'CROSS JOIN'? I thought you had to use WHERE
  • Yes @Cato INSERT INTO TABLE3 (code1, code2) SELECT T1.Code1, T2.Code2 FROM Table1 T1 CROSS JOIN Table2 T2 WHERE T2.code2 IN(2,3,4)