Hoppa till huvudinnehållet

SQL Query with a parameter

Kommentarer

3 kommentarer

  • Permanently deleted user

    Hi Marc,

    I have seen parameters working using a colon syntax. The [:status] in the command should get replaced with the value assigned to the status parameter.

    Command: select * from MyTable where Status = :status

    Parameter: (Name) status, (Value) "Open"

     

    0
  • Marc Rempel

    Thanks Paul,

    It looks like my problem was that I am trying to string together a dynamic query vs just setting a parameter for a specific field. 

    I ended gettting this to work by using some dynamic sql and using the @ instead of the : for the parameter.

    COMMAND:

    declare @sql nvarchar(max);

    set @sql = N'select distinct t.Topic, v.Topic_ID from VariableDesc as v join Topics as t on t.Topic_ID = v.Topic_ID where ' + @topic_filter;

    exec sp_executesql @sql;

    ----

    @topic_filter is set as a parameter that is equal to values like "DataSourceID in (2,3,4) and Geog in (1,2,3,)"

     

    Marc

    0
  • Frank Godino

    This is one of the few examples that I have seen for this SQL Query activity.  Thank you for the information.   I could Not see how the colon notation works.  Here is another

     

    declare @sql nvarchar(max);

    set @sql = N'SELECT MailToName1 AS Owner, MailLine1 AS Address, MailCity AS City, MailState AS State, MailZipCode AS ZipCode, PropID AS [Property ID] FROM V_PARCEL where  PropID IN ' + @PIDS + ' Union SELECT  ''Current resident'' as owner,  Formatted as address , SitusCity as city, ''CA'' as State, ZipCode, PropID as [Property ID] FROM            V_SITUS_ADDRESS_POINT where  TotalExemptionValue != 7000 AND PropID IN ' + @PIDS + ' order by PropID';

    exec sp_executesql @sql;

     

    The @PIDS is added as a String Parameter and initialized with a String Variable.

     

    0

Du måste logga in om du vill lämna en kommentar.