dynamic-sql

T-SQL: How to use parameters in dynamic SQL?

两盒软妹~` 提交于 2019-11-27 05:02:23
I have the following dynamic query which is working fine without the WHERE clause, which is expecting UNIQUEIDENTIFIER . When I pass it in, I don't get a result. I tried CAST and CONVERT , but no result. I might be doing it wrong, can anybody help? CREATE PROCEDURE [dbo].[sp_Test1] /* 'b0da56dc-fc73-4c0e-85f7-541e3e8f249d' */ ( @p_CreatedBy UNIQUEIDENTIFIER ) AS DECLARE @sql NVARCHAR(4000) SET @sql =' DECLARE @p_CreatedBY UNIQUEIDENTIFIER SELECT DateTime, Subject, CreatedBy FROM ( SELECT DateTime, Subject, CreatedBy, ROW_NUMBER() OVER(ORDER BY DateTime ) AS Indexing FROM ComposeMail WHERE

What is dynamic SQL?

坚强是说给别人听的谎言 提交于 2019-11-27 04:23:13
问题 I just asked an SQL related question, and the first answer was: " This is a situation where dynamic SQL is the way to go. " As I had never heard of dynamic SQL before, I immediately searched this site and the web for what it was. Wikipedia has no article with this title. The first Google results all point to user forums where people ask more or less related questions. However, I didn't find a clear definition of what a 'dynamic SQL' is. Is it something vendor specific? I work with MySQL and I

PostgreSQL convert columns to rows? Transpose?

℡╲_俬逩灬. 提交于 2019-11-27 04:04:38
I have a PostgreSQL function (or table) which gives me the following output: Sl.no username Designation salary etc.. 1 A XYZ 10000 ... 2 B RTS 50000 ... 3 C QWE 20000 ... 4 D HGD 34343 ... Now I want the Output as below: Sl.no 1 2 3 4 ... Username A B C D ... Designation XYZ RTS QWE HGD ... Salary 10000 50000 20000 34343 ... How to do this? Erwin Brandstetter Basing my answer on a table of the form: CREATE TABLE tbl ( sl_no int , username text , designation text , salary int ); Each row results in a new column to return. With a dynamic return type like this, it's hardly possible to make this

Ad hoc queries vs stored procedures vs Dynamic SQL [closed]

不问归期 提交于 2019-11-27 03:51:48
Ad hoc queries vs stored procedures vs Dynamic SQL. Can anyone say pros and cons? Bill Paetzke Stored Procedures Pro: Good for short, simple queries (aka OLTP--i.e. add, update, delete, view records) Pro: Keeps database logic separate from business logic Pro: Easy to troubleshoot Pro: Easy to maintain Pro: Less bits transferred over network (i.e. only the proc name and params) Pro: Compiled in database Pro: Better security (users don't need direct table access) Pro: Excellent query plan caching ( good for OLTP queries--benefits from plan reuse) Con: Excellent query plan caching ( bad for OLAP

Using a cursor with dynamic SQL in a stored procedure

断了今生、忘了曾经 提交于 2019-11-27 03:15:17
I have a dynamic SQL statement I've created in a stored procedure. I need to iterate over the results using a cursor. I'm having a hard time figuring out the right syntax. Here's what I'm doing. SELECT @SQLStatement = 'SELECT userId FROM users' DECLARE @UserId DECLARE users_cursor CURSOR FOR EXECUTE @SQLStatment --Fails here. Doesn't like this OPEN users_cursor FETCH NEXT FROM users_cursor INTO @UserId WHILE @@FETCH_STATUS = 0 BEGIN EXEC asp_DoSomethingStoredProc @UserId END CLOSE users_cursor DEALLOCATE users_cursor What's the right way to do this? cmsjr A cursor will only accept a select

Correctly inserting literals in PL/PgSQL EXECUTE dynamic queries

我怕爱的太早我们不能终老 提交于 2019-11-27 02:56:54
问题 The following is part of a plpgsql function. The problem is that the result of source_geom and target_geom is a character varying data type, and therefore I need to surround the both source_geom and target_geom in quotes(' '). The thing is that in plpgsql language how I don't know I can do it. Here's what I have at the moment: EXECUTE 'update ' || quote_ident(geom_table) || ' SET source = ' || source_geom || ', target = ' || target_geom || ' WHERE ' || quote_ident(gid_cname) || ' = ' || _r.id

Drop all tables whose names begin with a certain string

☆樱花仙子☆ 提交于 2019-11-27 02:44:07
I'd like a script to drop all tables whose name begins with a given string. I'm sure this can be done with some dynamic sql and the INFORMATION_SCHEMA tables. If anyone has a script, or can knock one up quickly, please post it. If no-one posts an answer before I figure it out myself, I'll post my solution. Curt Hagenlocher You may need to modify the query to include the owner if there's more than one in the database. DECLARE @cmd varchar(4000) DECLARE cmds CURSOR FOR SELECT 'drop table [' + Table_Name + ']' FROM INFORMATION_SCHEMA.TABLES WHERE Table_Name LIKE 'prefix%' OPEN cmds WHILE 1 = 1

dynamic sql query in postgres

独自空忆成欢 提交于 2019-11-27 02:03:58
I was attempting to use Dynamic SQL to run some queries in postgres. Example: EXECUTE format('SELECT * from result_%s_table', quote_ident((select id from ids where condition = some_condition))) I have to query a table, which is of the form result_%s_table wherein, I need to substitute the correct table name (an id) from an another table. I get the error ERROR: prepared statement "format" does not exist Link: string substitution with query result postgresql Craig Ringer EXECUTE ... USING only works in PL/PgSQL - ie within functions or DO blocks written in the PL/PgSQL language. It does not work

Dynamic SQL to generate column names?

那年仲夏 提交于 2019-11-27 01:05:15
I have a query where I'm trying pivot row values into column names and currently I'm using SUM(Case...) As 'ColumnName' statements, like so: SELECT SKU1, SUM(Case When Sku2=157 Then Quantity Else 0 End) As '157', SUM(Case When Sku2=158 Then Quantity Else 0 End) As '158', SUM(Case When Sku2=167 Then Quantity Else 0 End) As '167' FROM OrderDetailDeliveryReview Group By OrderShipToID, DeliveryDate, SKU1 The above query works great and gives me exactly what I need. However, I'm writing out the SUM(Case... statements by hand based on the results of the following query: Select Distinct Sku2 From

Use text output from a function as new query

[亡魂溺海] 提交于 2019-11-26 23:25:10
问题 In continuing from a previous case that was assisted by @Erwin Brandstetter and @Craig Ringer, I have fixed my code to become as follows. Note, that my function myresult() outputs now text , and not a table (as indeed, as was pointed out in the former case, there is no point in outputting a table object, since we would need to define all its columns ahead, which basically defies the entire purpose): CREATE OR REPLACE FUNCTION myresult(mytable text, myprefix text) RETURNS text AS $func$