dynamic-sql

How can I alter a sequence in dynamic SQL?

自作多情 提交于 2019-12-05 17:21:38
I'm trying to create a script to migrate data from one DB to another. One thing I'm not currently able to do is set the nextval of a sequence to the nextval of a sequence in another DB. I got the difference in values from user_sequences and generated the following dynamic SQL statements: execute immediate 'alter sequence myseq increment by 100'; execute immediate 'select myseq.nextval from dual'; execute immediate 'alter sequence myseq increment by 1'; commit; But nothing happens. What am I missing? If I run the same statements outside the procedure, they work fine: alter sequence myseq

Calling stored procedure that contains dynamic SQL from Trigger

可紊 提交于 2019-12-05 17:14:37
I am calling Stored procedure from Trigger and I get the following error: Dynamic SQL is not allowed in stored function or trigger Why is this happening, the dynamic SQL is being executed in Stored procedure, which is called from Trigger. Maybe this is the problem, if so is there any workaround? Edit (added code): Here is Trigger from the master table: -- Trigger DDL Statements DELIMITER $$ USE `TestaDataBase`$$ CREATE TRIGGER `TestaDataBase`.`UpdateAuxilaryTable` AFTER INSERT ON `MainTable` FOR EACH ROW BEGIN /* Here we call stored procedure with parameter id of newly inserted row. */ CALL

How to query table data by Object Id and Column Id?

一笑奈何 提交于 2019-12-05 13:53:22
Having the table Clients . PK LastName Name Address 1 Vidal Arturo St.... 2 Lavezzi Ezequiel St.... 3 Cuadrado Guillermo St.... I want to get: With the following query gives me the first four columns but how can I link this with the table data? SELECT TAB.object_id OBEJCTID, TAB.name TABLENAME, COL.column_id COLUMNID, COL.name FROM sys.tables TAB JOIN SYS.columns COL ON TAB.object_id = COL.object_id WHERE TAB.object_id = 25659888; You need to unpivot the data. Try something like this ;WITH cte AS (SELECT column_name, table_value FROM clients CROSS apply (VALUES ('pk',CONVERT(varchar(20),PK)),

Is there a way to select a database from a variable?

陌路散爱 提交于 2019-12-05 13:22:50
Is there a way to select a database from a variable? Declare @bob as varchar(50); Set @bob = 'SweetDB'; GO USE @bob Unfortunately, no. Unless you can execute the rest of your batch as dynamic SQL. Using execute to dynamically execute SQL will change the context for the scope of the execute statement, but will not leave a lasting effect on the scope you execute the execute statement from. In other words, this: DECLARE @db VARCHAR(100) SET @db = 'SweetDB' EXECUTE('use ' + @db) Will not set the current database permanently, but if you altered the above code like this: DECLARE @db VARCHAR(100) SET

Create dynamic select get value for column name - in SQL Server

随声附和 提交于 2019-12-05 13:21:49
Please help me create a select SQL statement with the results column name get from the column values in origin table (tablename is Device_Part ): User can input many DeviceCode which have many dynamic PartTypeName , the PartTypeName value is the PartInfo . This may help: CREATE Table Device ( DeviceCode NVARCHAR(100) NOT NULL, PartTypeName NVARCHAR(100) NOT NULL, PartInfo NVARCHAR(100) NOT NULL ) Insert Into Device Values('VT.SX-01','CPU','Pentium G6650'), ('VT.SX-01','Motherboard','H81M - S2PV'), ('VT.SX-01','RAM','DDR# 4GB - bus 1866 - Nano'), ('VT.SX-01','PartType Name 01','PartInfo 01') -

Cannot pass column name as parameter to sp_executesql

和自甴很熟 提交于 2019-12-05 12:07:22
I'm having trouble executing the below piece of code, it's giving me an error as below: Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '@ST'. I can try implementing the login using dynamic SQL, but wanted to try the sp_executesql method. Please let me know if I'm having some syntax error or I'm not supposed to pass table names as parameters? DECLARE @SQL NVARCHAR(4000)= ''; SET @SQL = N'--INSERT INTO #missingkeys ( SOURCE_KEY,[ROWCOUNT] ) SELECT S.[SOURCE_KEY], COUNT(1) AS [ROWCOUNT] FROM (SELECT DISTINCT @SK AS [SOURCE_KEY] FROM [PDA].@ST ) AS S LEFT JOIN [PDA].@MT AS T ON T.[SOURCE

How to Add Quotes to a Dynamic SQL Command?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 03:50:37
I am storing and editing some field in a database that involves a long string of one or more sentences. whenever i enter a single quote in the textbox and want to save it it throws an exception like "Incorrect syntax near 'l'. Unclosed quotation mark after the character string ''." is there any idea to avoid that? EDIT: The query is: SqlCommand com = new SqlCommand("UPDATE Questions SET Question = '[" + tbQuestion.Text + "]', Answer = '[" + tbAnswer.Text + "]', LastEdit = '" + CurrentUser.Login + "'WHERE ID = '" + CurrentQuestion.ID + "'"); As KM said, don't do this! Do this instead: private

How to solve ORA-29471 on dbms_sql.open_cursor?

£可爱£侵袭症+ 提交于 2019-12-05 02:16:59
I'm using Oracle 11.2.0.1.0 and am trying to get the dbms_sql package to work. However, I keep getting the ORA-29471 error, as shown below: DECLARE c INTEGER; BEGIN c := dbms_sql.open_cursor(); END; ORA-29471: DBMS_SQL access denied ORA-06512: at "SYS.DBMS_SQL", line 1017 ORA-06512: at line 4 The oracle docs say the following about this: Checks are made when binding and executing. Optionally, checks may be performed for every single DBMS_SQL subprogram call. The check is: The current_user is the same on calling the subprogram as it was on calling the most recent parse. The enabled roles on

Oracle EXECUTE IMMEDIATE with variable number of binds possible?

喜夏-厌秋 提交于 2019-12-05 01:14:33
I need to use dynamic SQL execution on Oracle where I do not know the exact number of bind variables used in the SQL before runtime. Is there a way to use a variable number of bind variables in the call to EXECUTE IMMEDIATE somehow? More specifically, I need to pass one parameter into the unknown SQL but I do not know how often it will be used there. I tried something like EXECUTE IMMEDIATE 'SELECT SYSDATE FROM DUAL WHERE :var = :var' USING 1; But it threw back with ORA-01008: not all variables bound. Steve Broberg You can't do this with EXECUTE IMMEDIATE . However, you can do this by using

What is a dynamic SQL query, and when would I want to use one?

半世苍凉 提交于 2019-12-05 00:05:43
What is a dynamic SQL query, and when would I want to use one? I'm using SQL Server 2005. Kyle Rozendo Here's a few articles: Introduction to Dynamic SQL Dynamic SQL Beginner's Guide From Introduction to Dynamic SQL : Dynamic SQL is a term used to mean SQL code that is generated programatically (in part or fully) by your program before it is executed. As a result it is a very flexible and powerful tool. You can use dynamic SQL to accomplish tasks such as adding where clauses to a search based on what fields are filled out on a form or to create tables with varying names. Dynamic SQL is SQL