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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:26:56+00:00 2026-05-11T02:26:56+00:00

The following code generates the primaey key for the new record to be inserted

  • 0

The following code generates the primaey key for the new record to be inserted and inserts the record into a table, whose name and the values to be inserted are given as parameters to the stored procedure. I am getting a runtime error. I am using Visual Studio 2005 to work with SQL Server 2005 Express Edition

ALTER PROCEDURE spGenericInsert  (     @insValueStr nvarchar(300),      @tblName nvarchar(10)  )   AS  DECLARE @sql nvarchar(400) DECLARE @params nvarchar(200) DECLARE @insPrimaryKey nvarchar(10) DECLARE @rowCountVal integer DECLARE @prefix nvarchar(5)  --following gets the rowcount of the table-- SELECT @rowCountVal = ISNULL(SUM(spart.rows), 0)     FROM sys.partitions spart      WHERE spart.object_id = object_id(@tblName) AND spart.index_id < 2  SET @rowCountVal = @rowCountVal+1     --Following Creates the Primary Key-- IF @tblName = 'DEFECT_LOG'      SET @prefix='DEF_' ELSE IF @tblName='INV_Allocation_DB'     SET @prefix='INV_' ELSE IF @tblName='REQ_Master_DB'     SET @prefix='REQ_' ELSE IF @tblName='SW_Master_DB'     SET @prefix='SWI_' ELSE IF @tblName='HW_Master_DB'     SET @prefix='HWI_'     SET @insPrimaryKey= @prefix + RIGHT(replicate('0',5)+ convert(varchar(5),@rowCountVal),5) -- returns somethin like 'DEF_00005'   -- Following is for inserting into the table --  SELECT @sql =   N' INSERT INTO @tableName VALUES ' +         N' ( @PrimaryKey , @ValueStr )'  SELECT @params = N'@tableName nvarchar(10), ' +                     N'@PrimaryKey nvarchar(10), ' +                     N'@ValueStr  nvarchar(300)'  EXEC sp_executesql @sql, @params, @tableName=@tblName, @PrimaryKey=@insPrimaryKey, @ValueStr=@insValueStr 

Output Message:

Running [dbo].[spGenericInsert] ( @insValueStr = 2,'Hi',1/1/1987, @tblName = DEFECT_LOG ).  Must declare the table variable '@tableName'.  No rows affected.  (0 row(s) returned)  @RETURN_VALUE = 0  Finished running [dbo].[spGenericInsert]. 
  • 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-11T02:26:56+00:00Added an answer on May 11, 2026 at 2:26 am

    You are going to have to concatenate the table name directly into the string, as this cannot be parameterized:

    SELECT @sql =   N' INSERT INTO [' + @tblName + '] VALUES ' +             N' ( @PrimaryKey , @ValueStr )'  SELECT @params = N'@PrimaryKey nvarchar(10), ' +                 N'@ValueStr  nvarchar(300)' 

    To prevent injection attacks, you should white-list this table name. This also isn’t robust if the table has other non-nullable columns, etc.

    note: Personally, though, I don’t think this is a good use of TSQL; it might be more appropriate to construct the command in the client (C# or whatever), and execute it as a parameterized command. There are use-cases for dynamic-SQL, but I’m not sure this is a good example of one.

    Better yet, use your preferred ORM tool (LINQ-to-SQL, NHibernate, LLBLGen, Entity Framework, etc) to do all this for you, and concentrate on your actual problem domain.

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • 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 There is probably a way to do this that is… May 11, 2026 at 3:33 pm
  • added an answer There is no 3.0 version of VB.Net. Are you refering… May 11, 2026 at 3:33 pm
  • added an answer Yes, it does. If the application is quitting, under certain… May 11, 2026 at 3:33 pm

Related Questions

I have a Silverlight application that communications with an ASP.NET backend through WCF. I
It seems like a dumb question, and yet. It could be my IDE that's
I have two tables, we'll call them Foo and Bar , with a one
I would like to confirm that the following analysis is correct: I am building

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.