dynamic-sql

Perform Operations on Dynamic Columns from Pivot

南笙酒味 提交于 2019-12-08 11:56:23
问题 As The Question says that I'm Trying to insert some values from dynamic sql Query to TempTable using Pivot, so the ExistingCoulmnName are not known to me In the next step I want to perform some arithmatic operation lets say multipliation on those Column Values. How Can I do that? Attaching Some samples : select @cols = STUFF((SELECT ',' + QUOTENAME(FYYear) from #TempCapEx group by FYYear order by FYYear FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT

SQL Server 2012 trigger: Execute dynamic sql for each row

爷,独闯天下 提交于 2019-12-08 11:31:54
问题 I have a trigger in a table which keeps all the changes (Insert, Update, Delete). When I insert only one row per time it works fine. But when I am trying to insert multiple rows at once I receive this error : Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Here is the code of the trigger ( I removed some parts that are not needed to shorten the code like variable declarations etc.) UPDATE:

Adding Grand Totals in Pivot Table

孤者浪人 提交于 2019-12-08 08:42:27
I have created a pivot table with the following code: DECLARE @SQL AS VARCHAR(MAX) DECLARE @Columns AS VARCHAR (MAX) SELECT @Columns = COALESCE(@Columns + ', ','')+ QUOTENAME(PortfolioID) FROM ( SELECT PortfolioID FROM InfoPortal.DBO.fn_Generic_PortfolioGroupMembers (@PortfolioGroup) ) AS B ORDER BY B.PortfolioID SET @SQL = ' WITH PivotData AS ( SELECT PortfolioID, Percentage, DurationBand, DurationSort FROM #Worktable ) SELECT DurationBand, ' + @Columns + ' FROM PivotData PIVOT ( SUM(Percentage) FOR PortfolioID IN (' + @Columns + ') ) AS PivotResult ORDER BY DurationSort' EXEC (@SQL) However

Oracle - cursor using dbms_utility.exec_ddl_statement not executing properly

余生长醉 提交于 2019-12-08 07:33:01
问题 I have a requirement to run a SP across multiple DBs at the same time and one part of it is to eliminate some duplicate records from each DB. Now since the SP can be run multiple times i've included a backup table and and what's needed to truncate and drop it in case the SP is run twice in a row. Now, since i'm creating tables via DBLINK i've researched that i need to use dbms_utility.exec_ddl_statement - but in this case even though the procedure executes, the truncate and drop queries seem

SELECT dynamic columns without functions in PostgreSQL

走远了吗. 提交于 2019-12-08 07:08:56
问题 I need to select rows from two and more tables ("A", "B"). They have differences columns and I don't use inheritance for it. So. For example: SELECT * FROM "A" UNION SELECT * FROM "B" ERROR: each UNION query must have the same number of columns I can understand why. I try get intersected columns from root schema in root table: SELECT column_name FROM information_schema.columns WHERE table_schema = 'client_root' AND table_name ='conditions' It's ok! But I don't use query: SELECT (SELECT column

sql server 2008 dynamic pivot based on ordinal value

纵然是瞬间 提交于 2019-12-08 01:26:15
问题 I have a UDF that splits a character string delimited by a space. As the function loops through each string if it finds a space and makes a split an ordinal number is inserted into the results table in its own column along with the split string. I have pivoted on this ordinal number but of course these numbers change based on the character string. I need to query these ordinal numbers and use them in my pivot. Could someone show me how this could be done with dynamic sql? I have seen examples

How do I execute sql text passed as an sp parameter?

六月ゝ 毕业季﹏ 提交于 2019-12-08 00:33:06
问题 I have a stored procedure with an nvarchar parameter. I expect callers to supply the text for a sql command when using this SP. How do I execute the supplied sql command from within the SP? Is this even possible?- I thought it was possible using EXEC but the following: EXEC @script errors indicating it can't find a stored procedure by the given name. Since it's a script this is obviously accurate, but leads me to think it's not working as expected. 回答1: Use: BEGIN EXEC sp_executesql @nvarchar

Dynamically define returning row types based on a passed given table in plpgsql?

佐手、 提交于 2019-12-07 18:31:27
I'm dynamically building a query in a plpgsql function that accepts a source table as an incoming variable. I want to return the results of the built SELECT statement, which performs aggregations on the given table, and returns results of that table. However, at the moment I'm getting the following error: ********** Error ********** ERROR: a column definition list is required for functions returning "record" SQL state: 42601 So it looks like I need to define column types of the record rows I want to return. I found this answer where you can bypass table type declaration by providing the table

PostgreSQL 9.3 trigger function to insert into table with parameterized name

北城以北 提交于 2019-12-07 16:15:17
问题 I'm trying to dynamically partition log entries in Postgres. I have 53 child tables (1 for each week's worth of log entries), and would like to route INSERTs to a child table using a trigger. I run the function with INSERT INTO log5 VALUES (NEW.*) , and it works. I run the function with the EXECUTE statement instead, and it fails. Within the EXECUTE statement, it's recognizing NEW as a table name and not a variable passed to the trigger function. Any ideas on how to fix? Thanks! The error:

Put $$ in dollar-quoted string in PostgreSQL

℡╲_俬逩灬. 提交于 2019-12-07 14:33:44
问题 I have a function in Postgres: CREATE OR REPLACE FUNCTION upsert(sql_insert text, sql_update text) RETURNS integer AS $BODY$ BEGIN EXECUTE sql_insert; RETURN 1; EXCEPTION WHEN unique_violation THEN EXECUTE sql_update; RETURN 2; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE COST 100; ALTER FUNCTION upsert(text, text) OWNER TO dce; I usually use this query to call that function: select upsert( $$INSERT INTO zz(a, b) VALUES (66, 'hahahaha')$$, $$UPDATE zz SET a=66, b='hahahaha' WHERE a=66$$ ) It works