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 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

Related Questions

I have the following code (generates a quadratic function given the a, b, and
The following code generates warning C4250. My question is, what's the best solution to
The following code generates a FileNotFoundException (using .NET 2.0): using System; using System.Collections.Generic; using
I don't understand why the following code generates a warning. interface Generic<T> { }
The following code snippet generates some warning messages when compiling: Cluster& Myclass::getCluster(const Point &p)
Class 1 has the following code that generates the exception - -(IBAction) searchAllAction: (id)
The following line of code generates the compile time error (PE19) There is no
One of the following pieces of code generates a memory leak, any idea which
I have the following SVG source code that generates a number of boxes with
I have a table with the following fields: id VARCHAR(32) PRIMARY KEY, parent VARCHAR(32),

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.