I have written a stored procedure to insert values into the table. But when I called the stored procedure twice, the duplicate values are being inserted into the table. How to avoid duplication of values in stored procedure.
Here is my stored procedure:
CREATE OR REPLACE PROCEDURE get_columns(TABLE_NAME VARCHAR)
RETURNS ARRAY
LANGUAGE JAVASCRIPT
AS
$$
var stmt = snowflake.createStatement({
sqlText: "SELECT * FROM " + TABLE_NAME + " LIMIT 1;",
});
stmt.execute();
var cols=[];
for (i = 1; i <= stmt.getColumnCount(); i++) {
INSERT INTO TABLE1 (Col_Name) VALUES ('+"stmt.getColumnName(i)"+'); //DUPLICATE VALUES
}
return cols
$$;
Edit:
I'm retrieving the column names of a Col_table say (Col1, Col2, Col3) and I want these column names into TABLE1.
I called the procedure call get_columns(Col_table) and the column names are inserted into TABLE1.
When I again call the procedure call get_columns(Col_table), TABLE1 will have duplicate values as (Col1, Col2, Col3, Col1, Col2, Col3). I want to avoid duplication either by using merge or update or if any other method.