dynamic-sql

sql use statement with variable

I'm trying to switch the current database with a SQL statement. I have tried the following, but all attempts failed: USE @DatabaseName EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']' To add a little more detail. EDIT: I would like to perform several things on two separate database, where both are configured with a...

Dynamic Linq To Sql With ComboBox and Column.Contains

I have a text box, combo box, button and DataGridView on a form that is used to search and return customer information from a MSSQL view (vCustomer). It works great, but I know my code can be more efficient. The four items in the combobox represent columns to search. Is there a simple way of converting the following to dynamic LINQ to...

Using a cursor with dynamic sql in a stored proc

I have dynamic sql statement I've created in a stored proc. 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 th...

Can I create a dynamic view of xml data in Sql Server 2005?

We have a situation where we need to store form data in our sql server but each new job we setup will have different fields with different field names and lengths. An example Job 1: Field 1: first_name - varchar(20) Field 2: last_name - varchar(30) Job 2: Field 1: first_name - varchar(15) Field 2: middle_initial - varch...

Unable to inject smalldatetime into D-SQL statement

Hi, when i try to execute this sql statement i am getting the error.. Conversion failed when converting character string to smalldatetime data type. Does anyone know what i am doing wrong? declare @modality varchar(50) declare @datefrom smalldatetime set @modality = 'xxxxxxx' set @datefrom = '20090101' declare @var1 nvarchar(4000) se...

How to Add Quotes to a Dynamic SQL Command?

I am storing and editing some field in a database that involves a long string of one or more sentences. whenever i enter a single quote in the textbox and want to save it it throws an exception like "Incorrect syntax near 'l'. Unclosed quotation mark after the character string ''." is there any idea to avoid that? EDIT: The query is: ...

Writing a reusable data iterator for SQL Server 2005/2008

i am trying to write a data iterator for SQL, it looks like the best approach is to cook up some dynamic sql for this problem. I want the iterator to support paging, sorting, and filtering of the data, and ideally not iterate over a memory copy but to not even select the data in the first place, perhaps LINQ to SQL or Entity Framework w...

How can I update multiple columns with a Replace in SQL server?

How do I update different columns and rows across a table? I want to do something similiar to replace a string in SQL server I want to do this but the value exists in multiple columns of the same type. The values are foreign keys varchars to an employee table. Each column represents a task, so the same employee may be assigned to severa...

Side effects of not including CommandType for dynamic sql?

What pitfalls may I encounter by not setting the cmd.CommandType attribute when running a dynamic sql call? I can not use adovbs.inc, and using cmd.CommandType = 200 yields the error: ADODB.Command (0x800A0BB9) Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. So by commenti...

How do I concatenate variables with a string to create an Oracle table name in a FROM clause in a stored procedure?

e.g. select * from v_schema || '.tbl_a@' || abc.world ...

Having some Dynamic SQL issues with INT type

Hello Im using SQL2000 so I build a Dynamic Query and in the last case I have this : IF (@apepac is not null and @nompac is not null and @month is not null ) SELECT @DynaSQL_1= @DynaSQL_1 + ' AND PACIENTE.apellidos like ''' + @apepac + '%'''+ ' AND PACIENTE.nombres like ''' + @nompac + '%'''+ ' AND DA...

dynamic ordering with nhibernate

Say you have a table named Articles. It has the following columns: ID, name, body, created, modified, pageviews, etc. Using a single method, would it be possible to pull a list of articles, and pass it which column I want to order by? And also the direction, i.e. desc or asc. ...

How to fetch named column from row parameter dynamically in Oracle PL/SQL?

I have a procedure like this: procedure foo( p_field_name in varchar2, p_record in table%rowtype ) is v_value varchar2( 100 ); begin execute immediate 'select p_record.' || p_field_name || ' from dual' into v_value; end Oracle complains that "p_record"."my_field_name" is an invalid identifier, which leads me to believe that th...

Outer join or dynamic query, which is the best way to go?

I have the following tables (I've simplified the data contained in the tables). RateTable - rate_table_id [int] - rate_table_name [nvarchar(50)] RateTableUsed - rate_used_id [int] - rate_table_id [int] (key to RateTable.rate_table_id) - customer_id [int] RateTableExtra - rate_table_extra_id [int] - rate_ extra_id [int] (key to...

[sp_executesql] Incorrect syntax near 'sp_executesql'

I don't understand why the following is giving me the error. I thought it was related to the commented out section, but @SQL is nvarchar(4000). BEGIN sp_executesql N'SELECT ''td''' --sp_executesql @SQL, N'@StartDate DateTime, @EndDate DateTime, @End2 DateTime, @Program varchar(4)', @StartDate, @EndDate, @End2, @Program END ...

How to create dynamic sql statment based on single XML parameter.

Hi, i need to create a dynamic SQL statement that selects fields based on an XML parameter. Say i have a stored proc with 1 param - [@FIELDS XML] that contains field names. eg... Field 1 = Name Field 2 = Address etc... ..in reality there would be up to 50 fields and i only want to report on the ones in the XML parameter. how can i m...

How to redefine Distinct

My boss has given me an assignment that I'm not sure is possible since after about two weeks, I can't figure out a solution, so I'm throwing this out to ask for any sort of help from the SO group. If this breaks your brain, I apologize. A little background first: We develop a database querying application that allows users to get back...

SQL Command not properly ended

I am getting the sql command not properly ended when hiting this line below. I want to insert into table A with data from table B. Both of them have the same columns but the sequence might be different. TIA! Insert into a (select column_name from user_tab_columns where table_name = 'B') select * from b I am using pl/sql developer. ...

pl sql and dynamic sql

Hi I am trying to create some dynamic sql using the following code block firstSqlStatement := true; updateText := 'UPDATE T_EMPLOYEES SET '; if FIRSTNAME IS NOT NULL and FIRSTNAME > 0 THEN updateText:=updateText || ' firstName=' || FIRSTNAME || ' '; firstSqlStatement := false; end if; if MIDDLENAME ...

JavaScript library for dynamically generating SELECT statements

Is there a good JavaScript library that dynamically generates SQL SELECT statements? Squiggle-Sql does this in Java. ...