dynamic-sql

save result of exec sp_excutequery using xpath

◇◆丶佛笑我妖孽 提交于 2019-12-12 22:19:35
问题 Hi i have this example DECLARE @XML1 xml, @XPath nvarchar(200), @EncodedXPath nvarchar(200), @Sql nvarchar(max), @val nvarchar(20) SET @XML1=' <Root> <Device> <Inspection> <Status>OK</Status> </Inspection> </Device> </Root>' SELECT @XML1.query('/Root[1]/Device[1]/Inspection[1]/Status[1]') SELECT @XML1.value('/Root[1]/Device[1]/Inspection[1]/Status[1]','varchar(5)') SET @XPath = '/Root[1]/Device[1]/Inspection[1]/Status[1]' SET @EncodedXPath = REPLACE(@XPath, '''', '''''') SET @Sql = N'SELECT

Trouble with Dynamic T-SQL IIF Statement

跟風遠走 提交于 2019-12-12 20:23:38
问题 This code works fine and does exactly what I want, which is to sum the Qty * Price for each instance of the dynamic query. But when I add an IIF statement it breaks. What I am trying to do is the same thing as above but when the transaction type is 'CO' set the sum to a negative amount. 回答1: The problem turned out to be the NVARCHAR(4000) type of @sql , limiting its length to 4000 characters: the query got truncated at some random place after adding another long chunk to it. DECLARE @sql

Executing queries dynamically in PL/pgSQL

不想你离开。 提交于 2019-12-12 19:19:07
问题 I have found solutions (I think) to the problem I'm about to ask for on Oracle and SQL Server, but can't seem to translate this into a Postgres solution. I am using Postgres 9.3.6. The idea is to be able to generate "metadata" about the table content for profiling purposes. This can only be done (AFAIK) by having queries run for each column so as to find out, say... min/max/count values and such. In order to automate the procedure, it is preferable to have the queries generated by the DB,

use variable in FROM statement

二次信任 提交于 2019-12-12 19:17:55
问题 I am trying to create a query that will be executed once for a specific table in a specific database in each server in sys.server . For each server.database.dbo.table I want to know the contents. So what I need is something like: declare @numrows int = (select count(*) from sys.servers) declare @i int = 1 while @i <= @numrows BEGIN declare @servername varchar(max) = (select servernaam from #servers where rij = @i) select * from @servername.DATABASE.DBO.TABLE set @i = @i+1 END However, the

SQL Server 2008 Updating VarChar Column with a UDF?

馋奶兔 提交于 2019-12-12 17:25:50
问题 I have a scalar function that takes a two variables @input1 @input2 and it returns the value of @input1 and @input2 (actual thing is more complex but this distills the idea). I want to update all rows in a table column using this function, passing the value 'abc ' for @input1 and using the column name in @input2, so my update statement would look something like: update mytable set mycolumn = (select dbo.myfunc( 'abc ' , mycolumn ) ) -- prepend the literal 'abc ' to every row for column

Selecting column name dynamically in an insert query

◇◆丶佛笑我妖孽 提交于 2019-12-12 13:03:31
问题 Getting error Invalid column name '@ColumnNames'. in the last line (insert clause), any idea why ? Declare @ColumnNames varchar(2000) Declare @OrderId int set @OrderId = 110077 select @ColumnNames = COALESCE(@ColumnNames + ', ', '') + COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='OrderItems' Insert into dbo.OrderHistory(@ColumnNames) select * from dbo.[Order] where ID= @OrderId 回答1: @ColumnNames is a string of text, not a list of columns. As a result, when you try to use it as

“ERROR: extra data after last expected column” when using PostgreSQL COPY

Deadly 提交于 2019-12-12 07:58:27
问题 Please bear with me as this is my first post. I'm trying to run the COPY command in PostgreSQL-9.2 to add a tab delimited table from a .txt file to a PostgreSQL database such as: COPY raw_data FROM '/home/Projects/TestData/raw_data.txt' WITH (DELIMITER ' '); I've already created an empty table called "raw_data" in the database using the SQL command: CREATE TABLE raw_data (); I keep getting the following error message when trying to run the COPY command: ERROR: extra data after last expected

Dynamic cursor Oracle

旧时模样 提交于 2019-12-12 06:59:46
问题 I want to create a dynamic cursor, but my code does not bring me the correct data. What am I doing wrong? DECLARE VAR1 VARCHAR2(500); CURSOR CUR1 IS SELECT T.COL1 FROM TABLE1 T WHERE T.COL1 IN (VAR1); BEGIN VAR1 := q'['V1','V2']'; FOR REG IN CUR1 LOOP DBMS_OUTPUT.PUT_LINE(REG.COL1); END LOOP; END; 回答1: In short, IN clause doesn't support bind variables.. It supports for only value,in the way you used.. You need to specify it like IN (var1, var2) ; Without knowing you , you have used bind

How to use UPDATE in PostgreSQL with variable table?

笑着哭i 提交于 2019-12-12 05:58:25
问题 Example: Table=["category1","category2","category3"] for varTable in Table: cr.execute('update varTable SET id=%s,WHERE id=%s) ........ .... How to do this loop? 回答1: Use dynamic SQL for that. The default is to use plpgsql with EXECUTE. Create a function or use a DO statement for ad-hoc execution. Dynamic SQL CREATE OR REPLACE FUNCTION f_up(_new_id int, _old_id int) RETURNS void AS $BODY$ DECLARE _tbl text[] := '{category1,category2,category3}'; t text; BEGIN FOREACH t IN ARRAY _tbl LOOP

Inserting Data Into Table Using Execute Immediate in Oracle

╄→гoц情女王★ 提交于 2019-12-12 04:24:50
问题 For example, I have some table "Test" which has one column "my_date" . What I am trying to do is adding record to a table using some variable: query_date := "SELECT sysdate FROM dual"; EXECUTE IMMEDIATE ('insert into test values query_date'); I need to insert the record to the table in this exact way by constructing string and executing query, however I get error. Is it possible to do so? 回答1: You can either get the result of the first query into a (date) variable and then use that: SELECT