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

  • Home
  • SEARCH
  • 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 156451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:19:49+00:00 2026-05-11T10:19:49+00:00

I feel like a dork just for asking this, but I’m not getting any

  • 0

I feel like a dork just for asking this, but I’m not getting any help from Google, and I paged through all of SO’s results on a simple search for SMO and didn’t see it either.

The short version is that I’m starting to play around with T4. I’m expanding on Oleg Sych’s initial tutorial to provide enumeration over all tables to create a (IMHO rather silly) delete proc. This is just an experiment, so its utter uselessness doesn’t bother me. 🙂

My expansion to Oleg’s tutorial looks like this:

<#@ template language='C#' hostspecific='true' #> <#@ output extension='SQL' #> <#@ assembly name='Microsoft.SqlServer.ConnectionInfo' #> <#@ assembly name='Microsoft.SqlServer.Smo' #> <#@ import namespace='Microsoft.SqlServer.Management.Smo' #> <#@ include file='T4Toolbox.tt' #> <#     // Config variables     string serverName = 'dbserver\\dbinstance';     string dbName = 'dbname'; #> USE <#= dbName #> <#       // Iterate over tables and generate procs     Server server = new Server(serverName);     Database database = new Database(server, dbName);      WriteLine('/* Number of tables: ' + database.Tables.Count.ToString() + ' */');      foreach (Table table in database.Tables)     {         table.Refresh(); #> CREATE PROCEDURE <#= table.Name #>_Delete <#         PushIndent('    ');         foreach (Column column in table.Columns)         {             if (column.InPrimaryKey)                 WriteLine('@' + column.Name + ' ' + column.DataType.Name);         }         PopIndent(); #> AS     DELETE FROM          <#= table.Name #>     WHERE <#         PushIndent('        ');         foreach (Column column in table.Columns)         {             if (column.InPrimaryKey)                 WriteLine(column.Name + ' = @' + column.Name);         }         PopIndent();         WriteLine('GO');     } #>  

The issue is that no tables are returned from the Tables collection. This is validated by the table count SQL comment I’m generating, which outputs 0.

As written, the code above generates the following:

USE dbname /* Number of tables: 0 */ 

However, if I remove the for loop and manually supply a valid table name which exists in this database, it generates the (again silly) proc — for that table.

The tables are separated into a schema, would that matter? Also, this is going against a SQL2005 instance — would that potentially cause issues?

Finally, I’m also finding that I can’t enumerate synonyms via the Synonyms collection. (I thought I’d be clever and go that route since the tables are in a schema, but have synonyms defined. But … no dice.)

Again, to reiterate, the above code is naturally not production, nor even production worthy. I’m just trying to learn both T4 and SMO, and hit a roadblock trying to do something which I’d thought would be ridiculously simple. 🙂

  • 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. 2026-05-11T10:19:50+00:00Added an answer on May 11, 2026 at 10:19 am

    SMO does not retrieve metadata automatically if you simply create a new instance of the Database class. Retrieving metadata can take a while, especially in a cold environment. Call database.Refresh() before the loop.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Depending on your folder structure and where the CSS is… May 11, 2026 at 1:47 pm
  • added an answer Well, I found out that CodeRush does this. May 11, 2026 at 1:47 pm
  • added an answer Take a look at the system stored procedure: sp_getapplock It… May 11, 2026 at 1:47 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.