dynamic-sql

Create a stored procedure in SQL Server that returns total count of columns in table and distinct count of values in each column

我只是一个虾纸丫 提交于 2021-02-17 07:24:16
问题 I need a stored procedure that takes any table name as a parameter and returns total count of rows and distinct count of values in each column of that particular table when executed. Let's take the sample table as below: create table journey ( Src varchar(255), Dest varchar(255) ) insert into journey values ('Jaipur', 'Mumbai'), ('Mumbai', 'Jaipur'), ('Kolkata', 'Bangalore'), ('Bangalore', 'Indore'),('Indore', 'Lucknow'), ('Lucknow', 'Indore') Can anybody help me with this task of dynamic SQL

ORA-00904: “Good”: invalid identifier ORA-06512

雨燕双飞 提交于 2021-02-17 05:37:28
问题 1.getting invalid identifier error while updating row_val variable char value 'Good' into the table 2.while inserting number it's working fine but strings its getting errors 3. declare count_temp number; csv_data varchar2(1000); val varchar2(100); row_val varchar2(100); sqlcmd varchar2(3000); col_name varchar2(100); col_data varchar2(100); begin csv_data:='good,son,r,,happy'; col_data:='varchar2(100)'; select regexp_count(csv_data, ',') + 1 into count_temp from dual; dbms_output.put_line

ORA-00904: “Good”: invalid identifier ORA-06512

两盒软妹~` 提交于 2021-02-17 05:37:27
问题 1.getting invalid identifier error while updating row_val variable char value 'Good' into the table 2.while inserting number it's working fine but strings its getting errors 3. declare count_temp number; csv_data varchar2(1000); val varchar2(100); row_val varchar2(100); sqlcmd varchar2(3000); col_name varchar2(100); col_data varchar2(100); begin csv_data:='good,son,r,,happy'; col_data:='varchar2(100)'; select regexp_count(csv_data, ',') + 1 into count_temp from dual; dbms_output.put_line

Filter column names from existing table for SQL DDL statement

醉酒当歌 提交于 2021-02-10 14:53:18
问题 Is it possible to filter on column names themselves in psql? I want to generate a limited version of the original table (with several hundred columns) in a separate schema a la (pseudocode): create table why.am_i_doing_this select * from original.table where column_name_of_the_table not in ('column_1', 'column_2' ); 回答1: Build the DDL command dynamically. You can do it in two steps: Build statement: SELECT 'CREATE TABLE why.am_i_doing_this AS SELECT ' || string_agg(column_name, ', ' ORDER BY

How to use & (ampersand) character in table name in dynamic sql?

拟墨画扇 提交于 2021-02-10 14:30:02
问题 I am using dynamic SQL to Select from a group of tables where some of the table names contain the '&' character. However once my query hits one of these tables I get the following error: Incorrect syntax near '&'. Here is some example code that recreates the error I am getting: DECLARE @TABLE_NAME AS NVARCHAR(150); SET @TABLE_NAME = 'A&BTable'; EXEC( 'SELECT col1, col2, col3 FROM Warehouse_Repository.dbo.' + @TABLE_NAME ) How can I alter this query so that I can Select from table names

How to use & (ampersand) character in table name in dynamic sql?

元气小坏坏 提交于 2021-02-10 14:29:04
问题 I am using dynamic SQL to Select from a group of tables where some of the table names contain the '&' character. However once my query hits one of these tables I get the following error: Incorrect syntax near '&'. Here is some example code that recreates the error I am getting: DECLARE @TABLE_NAME AS NVARCHAR(150); SET @TABLE_NAME = 'A&BTable'; EXEC( 'SELECT col1, col2, col3 FROM Warehouse_Repository.dbo.' + @TABLE_NAME ) How can I alter this query so that I can Select from table names

Oracle using dynamic sql when table name is a parameter

做~自己de王妃 提交于 2021-02-08 11:01:33
问题 I have a SQL query (a store procedure ) that i want to convert to PLSQL I already conver most of the store procedure but i cant convert the following part : DECLARE lookupTableRow CURSOR FOR SELECT TableName FROM SYS_LookUpTable OPEN lookupTableRow FETCH NEXT FROM lookupTableRow INTO @tableName WHILE @@FETCH_STATUS=0 BEGIN SET @sql='SELECT * FROM '+@tableName EXECUTE sp_executesql @sql IF @counter=0 BEGIN INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName) END ELSE

Oracle using dynamic sql when table name is a parameter

强颜欢笑 提交于 2021-02-08 11:00:57
问题 I have a SQL query (a store procedure ) that i want to convert to PLSQL I already conver most of the store procedure but i cant convert the following part : DECLARE lookupTableRow CURSOR FOR SELECT TableName FROM SYS_LookUpTable OPEN lookupTableRow FETCH NEXT FROM lookupTableRow INTO @tableName WHILE @@FETCH_STATUS=0 BEGIN SET @sql='SELECT * FROM '+@tableName EXECUTE sp_executesql @sql IF @counter=0 BEGIN INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName) END ELSE

Oracle using dynamic sql when table name is a parameter

岁酱吖の 提交于 2021-02-08 10:59:23
问题 I have a SQL query (a store procedure ) that i want to convert to PLSQL I already conver most of the store procedure but i cant convert the following part : DECLARE lookupTableRow CURSOR FOR SELECT TableName FROM SYS_LookUpTable OPEN lookupTableRow FETCH NEXT FROM lookupTableRow INTO @tableName WHILE @@FETCH_STATUS=0 BEGIN SET @sql='SELECT * FROM '+@tableName EXECUTE sp_executesql @sql IF @counter=0 BEGIN INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName) END ELSE

How do I use variables in a postgresql function for loop query

半世苍凉 提交于 2021-02-07 18:32:06
问题 I have a rather complicated function in postgresql (Version 9.4.4) that I need a bit of help with. I have a loop (with lots of work below) declared like this inside of my function: CREATE OR REPLACE function getRSI( psymbol varchar, pstarttime timestamp with time zone, pendtime timestamp with time zone, pduration double precision, ptable varchar ) RETURNS SETOF rsi AS $BODY$ declare row_data record; -- some variables begin FOR row_data IN SELECT datetime, value FROM "4" WHERE symbol = 'AAPL'