Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:18:01+00:00 2026-06-06T05:18:01+00:00

I am writing a stored proc to Select information, i would like it to

  • 0

I am writing a stored proc to Select information, i would like it to only select between dates?

This is what it looks like now:

ALTER PROCEDURE [dbo].[AutoCompleate] 
 @DateFrom datetime,
 @DateTo datetime,
 @SearchField varchar(50)
 AS

 -- V1.0 : ShaunM : 15 jun 2012
    --  AutoComplete textbox

 exec ('
 SELECT DISTINCT ' +
 @SearchField + ' FROM SchemaAudit 
             ORDER BY ' + @SearchField +' ASC')

I want the select to run for entry’s into the database between @DateTo and DateFrom, Does any one know how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:18:03+00:00Added an answer on June 6, 2026 at 5:18 am

    Instead of exec you should be using sp_executesql which allows for use of parameters, avoiding a risk of Sql injection and avoiding potential issues with dates passed as strings. First parameter is a query, second is a list of parameters and their types and the rest are parameter values.

    alter PROCEDURE [dbo].[AutoCompleate] 
         @DateFrom datetime,
         @DateTo datetime,
         @SearchField varchar(50)
         AS
    
         -- V1.0 : ShaunM : 15 jun 2012
            --  AutoComplete textbox
    
        declare @sql nvarchar(max)
        set @sql = 'SELECT DISTINCT ' 
                 + quotename(@SearchField)
                 + ' FROM SchemaAudit'
                 + ' WHERE [Date] between @from AND @to ORDER BY '
                 + quotename(@SearchField)
                 + ' ASC'
    
         exec sp_executesql @sql, 
                            N'@from datetime, @to datetime', 
                            @from = @DateFrom, @to = @DateTo
    

    Now, about start and end dates, what exactly you want to do?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a stored procedure where i m using try catch block. Now
I would like to execute a stored proc that simply updates a record. At
I would like to print messages from a SQL Stored Procedure. If I execture
I'm writing a stored proc in SQL Server 2008. The following code: SELECT @LastAccessed
This is my first shot at writing a stored procedure. I'm trying to get
So, I'm writing this Stored Proc and I really suck at SQL. My Question
i am writing stored procedures in MySQL that return values; CREATE PROCEDURE getCustomerById (id
I am writing a stored procedure that when completed will be used to scan
I am writing a stored procedure for displaying month and year. It is working,
I am writing a stored procedure in SQL Server 2008. Here I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.