Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

redshift SQL anyway to count the item number of a Select statement

Imagine there is a SQL statement with 300+ columns

create table if not exists (
300+ columns
);

Insert into
select
300 columns
from
a inner join b
on a.key=b.key 
;

It just keeps showing the error message

Invalid operation: INSERT has more expressions than target columns;

It is really hard to find which column is miss matching since there are too many columns.

Is there any way I can count the number of columns in a SELECT statement?

I know we can count the number of columns in information schema, but I want to count the number of columns/ items in a select statement, not an existing SQL table.

question from:https://stackoverflow.com/questions/65868270/redshift-sql-anyway-to-count-the-item-number-of-a-select-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, you can use information_schema tables. For instance, you could use:

create table tempt as
    select 300 columns
    from a inner join
         b
         on a.key = b.key;

(I would add something like limit 1 because you may not care about the data.)

Then you can look in information_schema.columns to get the columns lists in order, with their types. You can even compare the columns to the original table, using SQL statements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...