Dynamic Query Generation is not working with SQL

I am trying to execute following SQL statement :

USE[Northwind] declare @MainQuery varchar(255), @WhereClause varchar(255) Select @WhereClause = 'where CustomerId =ALFKI' Select @MainQuery ='Select * from Customers '[email protected] execute(@MainQuery)

But getting the error ,

Msg 207, Level 16, State 1, Line 1 Invalid column name 'ALFKI'.

What is wrong ?

-------------Problems Reply------------

ALFKI is being treated as if it is a column name. Your generated sql will look like this:

select * from Customers where CustomerID = ALFKI

What you want is your code to look like this:

select * from Customers where CustomerID = 'ALFKI'

To achieve this, change your where clause generator to this:

Select @WhereClause = 'where CustomerId = ''ALFKI'' '

Try this

Select @WhereClause = 'where CustomerId =''ALFKI'''

Category:sql Views:6 Time:2019-01-06
Tags: sql sql server

Related post

  • friendly_id does not work with sql server and tiny_tds? 2012-01-16

    I am using friendly_id 4.0.0 rails 3.0.1 and tiny_tds 0.2.3 (because I have a sql server database as a backend for the rails application) In my model I have extend FriendlyId friendly_id :friendly_name, :use => [:slugged] When I try to create a ne

  • CROSS APPLY does not work with SQL SERVER 2000? 2011-01-10

    How can I use something equivalent of CROSS APPLY in SQL Server 2000 ? I have a function which returns top level parent of id passed. ALTER Function [dbo].[fn_GetTopParentRiskCategory] ( @RctId int ) RETURNS @TEMP_TABLE TABLE ( rctId int, topParentRi

  • F# SqlDataConnection TypeProvider not working with Sql Server Ce 2012-03-09

    I'am playing around with the new F# type providers in VS 11 Beta in Win 8 CP and I can't convince the SqlDataConenction type provider to work with a SqlCe database. The code: open System open System.Data open System.Data.Linq open System.Data.SqlServ

  • Insert query in php not working with ajax 2012-04-11

    I am having some problems with insert query which is called from ajax. The ajax call comes back with success and I am able to see it with the changed html as noted below in the code under success:function(). I am not sure why the insert query in proc

  • MARS not working with sql native client 2012-05-02

    I have a C# application that export data to sql server using odbc connection. I got this error: System.Data.Odbc.OdbcException (0x80131937): ERROR [HY000] [Microsoft][SQL Native Client] Connection is busy with results for another command. I have trie

  • query object does not working with dynamic column name 2013-11-18

    qs_new = MODEl_NAME.objects.all() j=1 for query_value in qs_new: k=0 for field in model_obj._meta.fields: fieldname = field.get_attname_column() ws.write(0, k, fieldname[0]) ws.write(j, k, query_value.fieldname[1]) k= k +1 j=j+1 error at query_value.

  • Simple 301 redirect in .htaccess with query string does not work with Redirect directive 2010-08-14

    I am trying to redirect a single URL in a .htaccess file with Redirect: Redirect 301 /index2.php?option=com_rss&feed=RSS2.0&no_html=1 /something/somethingelse/ I have a bunch of other similar rules which work using directory structure URLs, b

  • C# Datagridview not working with SQL statement 2011-10-05

    I have a DataGridView to return result when user select date from DateTimePicker (DB field name =bdate) and Combobox (Bind from DB field name =sname) Everything working fine with only the DateTimePicker as selection. If I add in the "AND" SQL stateme

  • Powershell remotesigned policy not working with SQL agent Job, but working if run "manually" 2012-01-16

    I have a SQL 2008 job that does database backups using a Powershell script. Each step in the SQL job is "Operating system (CmdExec)" type. Each step kicks off a powershell script that performs SQL backup and other things. That script lives on a remot

  • VS2008 not working with SQL Server 2008 Express? 2009-11-06

    I've both VS2008 and SQL Server 2008 Express installed in my computer. When I try create a new DB or attach an existing one, I get the following error message:"This version of server is not supported - Only versions up to 2005 are supported." From th

  • Selection Formula Pass-through not working with SQL Command Object 2011-09-13

    I'm using Crystal Reports XI. We use Crystal Reports embedded into a share point site written in C#/ASP.net. Our web developers instatiate a report in code and pass the selection criteria by appending to the Select Formula object. The problem I'm run

  • Query Builder / DQL not working with INNER JOIN - Syntax Issue 2012-02-22

    I know I have a syntax isse here however I cant figure it out. I'm trying to do a SELECT and INNER JOIN of 5 tables but Symfony is complaining about the Entities in the JOIN are used before being defined. Actual error is as follows: [Semantical Error

  • Dynamically attached event handler not working with Update Panel 2010-04-22

    I have a checkbox inside a update panel. Based on certain conditions, I set the autopostback property to true and also attach a event handler as below chkBox.CheckedChanged += new EventHandler(this.ChkBoxChaned_Click); But when the checkbox is checke

  • MySQL QUERY HAVING clause not working with WHERE clause 2011-08-21

    I'm having issues with having clause and where clause I'm getting below error: #1054 - Unknown column 'accommodation' in 'where clause' Below is the code: SELECT b.bar_id, b.bar_name, b.bar_image, b.bar_lat, b.bar_lng, b.bar_address, b.bar_phone, b.b

  • Indexing does not work with LIKE and NOT operator in Sql Server, Is it a MYTH 2012-03-05

    I have seen in many articles on Sql Server which states that when we are writing a query we should avoid using NOT and LIKE operator in our query because indexing is not applied on it. eg. SELECT [CityId] ,[CityName] ,[StateId] FROM [LIMS].[dbo].[Cit

  • Is there a SqlFileStream like class that works with Sql Server 2005? 2010-01-22

    For the background to this question, see “How to I serialize a large graph of .NET object into a SQL Server BLOB without creating a large buffer?” that now has a large bounty on it. SqlFileStream gives you an IoStream that sits on top of a blob (varb

  • A LINQ extension that I wrote will not work with Entity Framework 2011-07-18

    I have an extension method defined like so: public static TSource MaxBy<TSource, TResult>(this IEnumerable<TSource> collection, Func<TSource, TResult> func) where TSource : class { var comparer = Comparer<TSource>.Default; TSo

  • Order by does not work with Concat() in LINQ 2011-08-08

    Using VB.net and the following LINQ statement. I suspect the "Order by" does not work with Concat(). I want to list the current item the user has and then list more available items in asending order. So first i select the current item from the db and

  • Jar file does not work with database (Hibernate) 2011-12-25

    I have created an application using hibernate... It works fine with database when I run it through Netbeans. But when I build application (using Clean and Build tool) it does not work with database. Error: WARNING: SQL Error: 40000, SQLState: 08001 M

  • Count (*) not working with where clause 2012-02-03

    have an issue with a fairly basic query I'm attempting to write. select count (p_id) as mycustomer from person where date_active = null; Which doesn't work. (runs, but returns 0) However, the below statement does (and returns a number), can anyone he

  • Access Query Criteria Suddenly Not Working 2012-07-13

    I have a query with several SQL 2005 relational tables linked to one Visual FoxPro table in an Access database. I also have criteria on one of the Primary Keys of one of the Access tables that is a reference to a field on a form (forms!frmTruckload!m

  • Windows 7 Upgrade advisor says my MSI NX6600-TD256E video card will not work with the Aero user interface, yet the specifications of my card say that it should work. 2015-02-22

    Windows 7 Upgrade advisor says my MSI NX6600-TD256E video card, or "graphics interface" as they seem to now be called, will not work with the fancy Aero user interface, yet the specifications of my card say that it should work. I have done the firmwa

  • How do you work with SQL Server CE and SQL Server 2008 Express? 2009-01-22

    I have installed: Visual C# 2008 Express Visual Web Developer 2008 Express Visual Studio 2008 SQL Server 2008 Express SQL Server Management Studio In Visual C# 2008 Express, I can "Add Item" and create a "Local Database" (.sdf file) or a "Service Bas

  • How to make Java work with SQL Server? 2009-08-21

    I know this is a basic question, but I can't seem to find an answer and I apologize, if this question is way too stupid, but here we go: I am supposed to work with SQL Server (no problem so far) and with Java (love java, so no problem here either), b

  • How linq to sql works with sql connections? 2010-01-21

    What is the best way of working with linq data context? And how does link work with sql connection (when does it open connection and when does it close connection)? Should I aloways use using statement when i work with data context? Sorry for list of

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.095 (s). 11 q(s)