dynamic-sql

Dynamic SQL with table name as a parameter

瘦欲@ 提交于 2019-12-12 03:39:37
问题 I am trying to execute a procedure into which i send the table name and 2 column names as parameters: EXECUTE IMMEDIATE 'select avg(@column1) from @Table1 where REF_D = @column2' into ATTR_AVG; I have tried using the variables in combiations of '@' , ':' , '||' but nothing seems to work. Has anyone used table names as a parameter. there are a few solutions here but for SQL Server 回答1: You can only use bind variables (denoted by colons) for values, not for parts of the structure. You will have

Oracle Dynamic 'SQL select' query record type

假如想象 提交于 2019-12-12 01:47:12
问题 The dynamic request looks like this: request := 'select * from ' || param_table_name || ' where ' || column_name_list; Then I do: open cur1 for request; The exact type of record fetched via cur1 isn't known until runtime, because it should impose no restrictions on whatever table this procedure is applied to. But then, how do I iterate through what the query returns? 回答1: PL/SQL cursors cannot work with columns that aren't known a compile time. To only way to work with fully dynamic queries

EXECUTE IMMEDIATE with USING clause giving errors

寵の児 提交于 2019-12-11 23:18:34
问题 All, I am very new to stored procedures in general but I am struggling especially with those in Oracle. I have created a very simple example of what I am trying to accomplish and I am still getting the same error with this simplified version. The example stored procedure is as follows: CREATE OR REPLACE PROCEDURE ashish_test AUTHID CURRENT_USER IS BEGIN DECLARE v_tab VARCHAR2(50); v_strSQL VARCHAR2(50); BEGIN v_strSQL := 'SELECT * FROM :1'; v_tab := 'ex.emp'; EXECUTE IMMEDIATE v_strSQL USING

Dynamic SQL Query Search

廉价感情. 提交于 2019-12-11 21:13:35
问题 I'm trying to build an SQL query with given params, but I get a weird error and cant understand why. Here is my SP and result ALTER PROCEDURE [dbo].[sp_Photographers_Select_Search] @Date varchar(100), @PriceMin int, @PriceMax int, @CityID int AS BEGIN SET DATEFORMAT DMY DECLARE @SQL as varchar(2000) SET @SQL = 'SELECT *, (SELECT TOP (1) Price FROM Packages WHERE PhotographerID = Photographers.PhotographerID ORDER BY Price) as PriceMin, (SELECT TOP (1) Price FROM Packages WHERE PhotographerID

SQL Updating Rows Without Knowing Column Name

不打扰是莪最后的温柔 提交于 2019-12-11 19:22:14
问题 I am attempting to create a stored procedure that will update a specific row and column location based on user input. My table has approximately 100 columns utilizing names in progressive increments. Ex Page1, Page2, Page3, Page 4.... etc. The data must also be updated in progressive increments as users finish different versions of each page. When the procedure is called I need it to find the user's row(Page1 is the key and unique), and put the information in where their version of the file

Dynamic SQL for updating a table from ASP .NET

给你一囗甜甜゛ 提交于 2019-12-11 18:50:31
问题 I have an ASP.NET 3.5 application that I want to allow the user to select a table and allow CRUD operations on that table. The user will be restricted to a number of tables to edit however the tables and even database won't be known until after deployment; the web.config will setup the connection and tables. So I need to build a framework that will allow a generic table in a SQL database to be updated. Most of the ORMs I've seen require the schema to be known at compile time so I cannot use

Retrieve number of rows updated

女生的网名这么多〃 提交于 2019-12-11 17:46:24
问题 Below Dynamic SQL is updating 1319 rows. l_sql := 'UPDATE '||l_prefix||'CRS_CUSTOMERS SET CUSTOMER_SOURCE_REF_ID = :REF_ID EXECUTE IMMEDIATE l_sql USING i.CUSTOMER_REF_ID, i.CUSTOMER_ID; TO_CHAR(SQL%ROWCOUNT); The rowcount output is just one? How can i use any script to retrieve actual number of rows affected? 回答1: Your code should be like this: l_sql := 'UPDATE '||l_prefix||'CRS_CUSTOMERS SET CUSTOMER_SOURCE_REF_ID = :REF_ID'; EXECUTE IMMEDIATE l_sql USING i.CUSTOMER_REF_ID, i.CUSTOMER_ID;

Dynamically search columns for given table

浪子不回头ぞ 提交于 2019-12-11 17:39:09
问题 I need to create a search for a java app I'm building where users can search through a SQL database based on the table they're currently viewing and a search term they provide. At first I was going to do something simple like this: SELECT * FROM <table name> WHERE CAST((SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table name>') AS VARCHAR) LIKE '%<search term>%' but that subquery returns more than one result, so then I tried to make a procedure to loop through all

Adding an apostrophe into a dynamic SQL

限于喜欢 提交于 2019-12-11 15:17:28
问题 I would like to ask how can I add an apostrophe into a dynamic SQL. I need to return an SQL statement in one of the columns which has to have apostrophes in itself. I have the following statement: SET @SQL_String = N'INSERT INTO #ReturnTable ( TableName, ColName, SQL_Statement, Value ) VALUES ( ''' + @TableName + ''', ''' + @ColName + ''', ''' + 'SELECT ' + @ColName + ' FROM ' + @TableSchema + '.' + @TableName + ' WHERE ' + @ColName + ' = ' + CAST(@GuidArgument AS NVARCHAR(50)) + ';' +''', (

Store XML Tags in Table for Different XML Size in SQL Server

◇◆丶佛笑我妖孽 提交于 2019-12-11 14:45:54
问题 I have code in SQL Server which extracts the tags from XML and stores them in a temp table (#result). Right now the cursor runs in a loop for each XML assuming that structure of all XMLs is same. Example below: XML1 : <Root><Tag1>val1</Tag1><Tag2>val2</Tag2><Tag3>val3</Tag3></Root> result Table: Tag1 | Tag2 | Tag3 --------|-----------|-------- val1 | val2 | val3 But now we have some XMLs which can have less number of tags. Example below: XML1 : <Root><Tag1>val1</Tag1><Tag2>val2</Tag2><Tag3