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 8759355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:46:53+00:00 2026-06-13T14:46:53+00:00

How can I script the creation of a new SQL Login (whether by Windows

  • 0

How can I script the creation of a new SQL Login (whether by Windows
Authentication or with SQL password) and have it automatically expire after some
time, e.g. 8 hours, 24 hours

I’ve tried searching here in stackoverflow but I haven’t found a solution. If you guys know of one that exists here, please let me know too. Thank you.

I am using SQL Server 2005 and 2008

  • 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-13T14:46:55+00:00Added an answer on June 13, 2026 at 2:46 pm

    First create a table to store the auto-expiring logins

    create table master.dbo.logins(
        id int identity primary key clustered,
        login_name sysname not null unique,
        valid_from datetime not null,
        valid_to datetime not null,
        check (valid_from < valid_to)
    )
    GO
    

    Create a SERVER LEVEL LOGON trigger to capture and block the special logins. Notice that I am specially testing for the prefix XPR as a convention, because if you get a LOGON trigger wrong, it’s inconvenient to fix.

    CREATE TRIGGER deny_expired_logins
    ON ALL SERVER
    with execute as 'sa'
    FOR LOGON
    AS
    BEGIN
        if original_login() like 'XPR%' and not exists (
            select *
              from master.dbo.logins
            where login_name = original_login()
              and GETDATE() between valid_from and valid_to)
            rollback
    END
    GO
    

    When you need to create new logins, remember to insert a record into the table.

    create login XPR00001 with password = 'ABCD1234..';
    insert master.dbo.logins select 'XPR00001', getdate(), dateadd(hh,8,getdate());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: javascript date creation, can't set the correct month I have script like:
EDIT: The entire code and database creation script can be found from http://gitorious.org/scheator .
I have a Python3 script that can take optional command line parameters which the
I'm trying to write a simple PHP script which automatically sets up new etherpads
In WebSphere MQ 6, I want to script the creation of new queues. However
I was wondering if I can switch the connected user within an sql script
Let's assume that I can't script the loaded swf, nor can the loaded one
I am aware that you can 'remotely' script android using port forwarding, using the
Here is a blog post from Kirk Munro that explains how a script can
Hi and Good day to all, Google App Script can be used in so

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.