dynamic-sql

How to INSERT INTO table from dynamic query?

空扰寡人 提交于 2019-12-13 16:13:50
问题 My version of Postgres is: "PostgreSQL 9.4.4, compiled by Visual C++ build 1800, 32-bit" Let's say I have two tables Table1 and Table2 , which are having column col1 and col2 respectively. CREATE TABLE Table1(col1 int); CREATE TABLE Table2(col2 int); There is another table Table3 storing a formula for migrating data from Table1 to Table2 : CREATE TABLE Table3 ( tbl_src varchar(200), col_src varchar(500), tbl_des varchar(200), col_des varchar(100), condition varchar(500) ); INSERT INTO Table3

DROP All Views PostgreSQL

随声附和 提交于 2019-12-13 14:46:17
问题 How I can Delete All User Defined Views From PostgreSQL using a Query? Like we can delete All functions using query : SELECT 'DROP FUNCTION ' || ns.nspname || '.' || proname || '(' || oidvectortypes(proargtypes) || ');' FROM pg_proc INNER JOIN pg_namespace ns ON (pg_proc.pronamespace = ns.oid) WHERE ns.nspname = 'my_messed_up_schema' order by proname; 回答1: Script for deleting all views in a certain schema: SELECT 'DROP VIEW ' || t.oid::regclass || ';' -- CASCADE? FROM pg_class t JOIN pg

Dynamic SQL Result INTO #Temp Table

空扰寡人 提交于 2019-12-13 09:10:14
问题 I have to Execute a dynamic SQL SELECT Query And put Results into a #TempTable . DECLARE @StateId CHAR(3)='StateID'; DECLARE @DeptId CHAR(15)='DeptID'; DECLARE @Query VARCHAR(MAX)='Select Columns With Joins Passed From Front End' DECLARE @Where VARCHAR(500); DECLARE @FinalQuery VARCHAR(MAX)=''; SET @Where='Some Where Condition'; SET @FinalQuery='SELECT '+@Query+' '+@Where+'' EXEC(@FinalQuery) -- Want To INSERT THIS Result IN SOME `#TempTable` -- SO that I can perform Some Operations On `

Writing Dynamic SQL Statement

好久不见. 提交于 2019-12-13 06:50:05
问题 I am very new to Microsoft Access. I would like to select a column from a table based on the value of a parameter (i.e. my table has columns x, y and z and I have a chosencol parameter which is set by the user using a dropdown. I can select one / all of the columns using a select command, however, I would like to do this using my parameter chosencol instead. Having read around, I have found a number of references to using the SET and EXEC commands, however, entering them into the SQL command

Dynamic creation of table in tsql

守給你的承諾、 提交于 2019-12-13 06:39:07
问题 I am trying to create a temptable dynamically in tsql . I have a query which gives me some data (as a result of another dynamic query ) in a column. For example say years like 2011/2012 . This will be in a column. What i want is to pick value of each row and name it as a column name of type int. Later , i can use that temp table to dump data into it. Any suggestions? 回答1: Would you be able to use something like this? DECLARE @Tab VARCHAR(MAX) SET @Tab = 'CREATE TABLE #Whatever (' SELECT @Tab

Can I truncate tables dynamically?

空扰寡人 提交于 2019-12-13 04:00:58
问题 I have a table with 3 different partitions that is split on the point_of_sale column. I have constructed a view ( massive_table ) to aggregate data from these partitions like so: CREATE VIEW massive_table as SELECT * FROM massive_table_UK UNION ALL SELECT * FROM massive_table_US UNION ALL SELECT * FROM massive_table_DE; The massive_table_UK/US/DE tables each have a check constraint imposed on them so that the point_of_sale column can only contain a string value of either UK , US or DE as

Alternative to executing dynamic sql

≯℡__Kan透↙ 提交于 2019-12-13 03:56:08
问题 I currently have a 'Filter' object which corresponds to a business object. This object has properties that relate to the different ways that I want to be able to filter/search a list of such business objects. Currently these Filter objects have a method that builds the contents of a where-clause that is then passed to a SQL Server 2000 stored procedure where it is concatendated with the rest of the select query. The final string is then executed using Exec . Currently this works fine except I

INSERT INTO temp table from unknown number and name of columns (from dynamic PIVOT query)

孤者浪人 提交于 2019-12-13 03:36:14
问题 I have a dynamic query as shown below. The @ColumnNames param has multiple columns this pivot is using. @ID and @Apartment_ID come from insert parameters. SET @DynamicSQL = 'select id, name, address, phone, remarks, ' + **@ColumnNames** + ' from (select b.id, name, criteria_id, impact_value, remarks from dbo.User u inner join dbo.ID b on b.id = u.id where b.Instance_ID = '+ **@Id** + 'and ownerID in (select * from fnSplitString(''' + **@Apartment_ID** + + ''',' + ''',''' + '))' + ') as t

Using dynamic sql in a procedure to create schemas and tables

好久不见. 提交于 2019-12-13 03:19:26
问题 I've been tasked with creating a sample database that stores data from my college's 15 campuses. The data from each campus must be separate from the rest (with the use of schemas) and each schema must have the same tables and columns. This is where dynamic sql has to be used (as stated in the assignment). The following code snippet demonstrates my efforts (bare in mind that I'm still new to this): USE master GO CREATE DATABASE CTUDB GO USE CTUDB GO CREATE PROCEDURE AddCampus_proc(@campus

Conversion failed when converting datetime from character string

这一生的挚爱 提交于 2019-12-13 01:02:38
问题 I am trying to pass parameters from windows application to a stored procedure. there are two parameters '@dt' & '@dt2' which get value from datetimepickers. whenever i execute the stored procedure it comes up with the error saying "Conversion failed when converting datetime from character string." After executing the query i want to display it in a datagridview. i am unable to figure out the problem.. here is my procedure and code Stored Procedure ALTER proc [dbo].[deepak_proc] ( @dt datetime