dynamic-sql

Select a column returned by a stored procedure

I have a stored procedure which is returning me about 50 columns. I want to write a query, where I will be able to select a particular column from the list of column returned by the SP. I tried writing select RSA_ID from exec(uspRisksEditSelect '1') But Its throwing me an error. I think we need to write some dynamic sql for it. But I am...

Creating SQL table using Dynamic variable name

I want to create backup SQL tables using variable names. something along the lines of DECLARE @SQLTable Varchar(20) SET @SQLTable = 'SomeTableName' + ' ' + '20100526' SELECT * INTO quotename(@SQLTable) FROM SomeTableName but i'm getting Incorrect syntax near '@SQLTable'. It's just part of a small script for maintence so i ...

T-SQL Dynamic SQL and Temp Tables

It looks like #temptables created using dynamic SQL via the EXECUTE string method have a different scope and can't be referenced by "fixed" SQLs in the same stored procedure. However, I can reference a temp table created by a dynamic SQL statement in a subsequence dynamic SQL but it seems that a stored procedure does not return a query r...

Invoking a function call in a string in an Oracle Procedure

Hello, I writing an application using Oracle 10g. I am currently facing this problem. I take in "filename" as parameter of type varchar2. A sample value that filename may contain is: 'TEST || to_char(sysdate, 'DDD')'. In the procedure, I want to get the value of this file name as in TEST147. When i write: select filename into ffilena...

Ad hoc queries vs stored procedures vs Dynamic SQL

Ad hoc queries vs stored procedures vs Dynamic SQL. Can anyone say pros and cons? Best regards, Kristaps ...

Explain FOR in oracle

I am making a test. I have all tests in rows, so my rows looks like this; ID | TEST ---------------------------------- 1 | 'select sysdate from dual' 2 | 'select sysdatesss from dual' Now I read it row by row and I need to test it with EXPLAIN PLAN FOR so the for the first row it would be EXPLAIN PLAN FOR select sysdate fro...

select columns by a concat text as columnname in oracle

I have a table with columns named with the number of hour of day like this: col00 NUMBER(5) col01 NUMBER(5) col02 NUMBER(5) ... col23 NUMBER(5) ...and I have another query that returns a count by hour. I want to recover the colXX value by hour.... then I can recover with "decode" or "case when..." but I want know if exists any way to...

How to handle an empty result set from an OpenQuery call to linked analysis server in dynamic SQL?

I have a number of stored procedures structured similarly to this: DECLARE @sql NVARCHAR(MAX) DECLARE @mdx NVARCHAR(MAX) CREATE table #result ( [col1] NVARCHAR(50), [col2] INT, [col3] INT ) SET @mdx = '{some dynamic MDX}' SET @sql = 'SELECT a.* FROM OpenQuery(LinkedAnalysisServer, ''' + @mdx + ''') AS a' INSERT INTO #result EXE...

Why I can't use template table in dynamic query SQL SERVER 2005

Hello! I have the following t-sql code which generates an error Declare @table TABLE ( ID1 int, ID2 int ) INSERT INTO @table values(1, 1); INSERT INTO @table values(2, 2); INSERT INTO @table values(3, 3); DECLARE @field varchar(50); SET @field = 'ID1' DECLARE @query varchar(MAX); SET @query = 'SELECT * FROM @table WHERE ' + @f...

Dynamic SQL and Functions

Hi all, is there any way of accomplishing something like the following: CREATE FUNCTION GetQtyFromID ( @oricod varchar(15), @ccocod varchar(15), @ocmnum int, @oinnum int, @acmnum int, @acttip char(2), @unisim varchar(15) ) AS RETURNS DECIMAL(18,8) BEGIN DECLARE @Result decimal(18,8) DECLARE @SQLStrin...

Dynamically Specify Linked Server and DB Names in Stored Procedure

I have the same query in a stored procedure that needs to be executed on different servers and databases according to parameters. How can I arrange this without exec or sp_executesql? I'm using SQL Server 2008. Thank you. UPDATE I've found some links http://www.eggheadcafe.com/software/aspnet/29397800/dynamically-specify-serve....

How to design a generic database whose layout may change over time?

Here's a tricky one - how do I programatically create and interrogate a database whose contents I can't really foresee? I am implementing a generic input form system. The user can create PHP forms with a WYSIWYG layout and use them for any purpose he wishes. He can also query the input. So, we have three stages: a form is designed an...

Dynamic order by without using dynamic sql ?

I have the following stored procedure which can be sorted ascending and descending on TemplateName,CreatedOn and UploadedBy. The following SP when runs doesnot sort records.if i replace 2,3,4 by columnname, i got an error message "Conversion failed when converting the nvarchar value 'Test Template' to data type int.".Please suggest how t...

In SQL Server, how do I create a reference variable to a table?

I'm currently using sp_executesql to execute a T-SQL statement with a dynamic table name. However, it is really ugly to see something like: set @sql = 'UPDATE '+Table_Name+' SET ... WHERE '+someVar+' = ... AND '+someVar2' = ...' sp_executesql @sql What I would rather like to have is a TABLE variable of which is a reference to a table...

Avoiding branching in stored procedures that return search results?

I have an application that needs to return search results from a SQL Server 2008 database. I would like to use a single stored procedure to return the results but I am finding that as I build the stored procedure it is full of many Else .. Else If statements with the query repeated over and over with slight variations depending on the us...

Writing dynamic SQL queries

I am having problems trying to create a fairly simple dynamic SQL query. The variables don't display the values they contain when I PRINT @SQLString. Any ideas? ALTER PROCEDURE [dbo].[usp_ItemSearch] @ItemNum varchar(30) = NULL ,@SearchFilter int ,@VendorNum varchar(10) = NULL ,@RecUserID int = NULL ,@StartDate s...

How do I pass a column as a parameter in Stored Procedure?

How do I pass and use the column name to retrieve a bigint variable in the actual column? DECLARE @personID BIGINT, DECLARE @queryString varchar(500) Set @queryString = 'Select @personID = ' + @PersonColumnID + ' from dbo.Loss_Witness where WitnessID = @witnessID' exec(@queryString) Error message states "Must declare variable '@perso...

SQL Variable Column Name depends on Row data

I am tryin to get a column from DB that returns Variable Column Name which depends on Row data. I know I can have variable Column name with using Dynamic SQL, but what if the name actually depends on the row's information. SELECT name,age FROM dbo.Names --Reurns 'name' as column name SELECT name as [xyz],ag...

Writing a dreaded SQL search query (2nd phase)

I am working on a search query (with an asp.net 3.5 front end) which seems quite simple, but is quite complex. The complete query is: set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[usp_Item_Search] @Item_Num varchar(30) = NULL ,@Search_Type int = NULL ,@Vendor_Num varchar(10) = NULL ,@Search_User_I...

In SQL Server, is there a way to avoid using a Cursor?

I have a table where each record has a Table_Name (name of a table). I then use a Cursor to select all table names related to some record in to a Cursor. Then I do a WHILE for each table name in the Cursor to do some job. I want to know if it's possible to solve this problem without using a Cursor. DECLARE tables_cursor CURSOR FAST_FOR...