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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:11:55+00:00 2026-06-12T10:11:55+00:00

I’m using SQL Server 2008 R2 (T-SQL) and have dynamic SQL code, which inserting

  • 0

I’m using SQL Server 2008 R2 (T-SQL) and have dynamic SQL code, which inserting into temporary table some data.
If I run code in new query window or PRINT code and then run in new query window everything is allright, BUT if I try to run this from stored procedure it throw error

Msg 156, Level 15, State 1, Procedure users, Line 3
Incorrect syntax near the keyword 'FROM'.

If I remove line with INSERT INTO #viewsTmp (webId, listId, viewName, viewTitle), code runs fine.
I have similar code above within same stored procedure and that code is fine.

Temp table:

CREATE TABLE #viewsTmp 
(
    webId uniqueidentifier,
    listId uniqueidentifier,
    viewName NVARCHAR(128),
    viewTitle NVARCHAR(255)
)

variable @databaseName is declared as parameter of stored procedure

@databaseName NVARCHAR(255)

Code which is OK in new query window, but not ok if is runned from stored procedure:

SET @sql = N'
INSERT INTO #viewsTmp (webId, listId, viewName, viewTitle)
SELECT c.tp_WebId, c.tp_ID, b.LeafName, c.tp_Title
FROM (
    SELECT *
    FROM (
        SELECT [ListId], [LeafName], [Type], [WebId]
              ,ROW_NUMBER() OVER (PARTITION BY ListId ORDER BY DirName) AS RowNumber
          FROM ['+@databaseName+'].[dbo].[Docs]
          WHERE [LeafName] = ''users''
    ) AS a
    WHERE  [RowNumber] = 1 AND [Type] = 1
) AS b
INNER JOIN 
    (SELECT [tp_WebId], [tp_ID], [tp_Title] FROM ['+@databaseName+'].[dbo].[Lists]) 
    AS c 
    ON b.ListId = c.tp_ID AND b.WebId = c.tp_WebId

';
--PRINT @sql
exec sp_executesql @sql;

What is wrong with this code?

  • 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-12T10:11:57+00:00Added an answer on June 12, 2026 at 10:11 am

    I decomposed and composed code step by step with small modification as Dale Burrell advised….and code runs and don’t runs. I didn’t know what was that…and finally I discover one my mistake…

    Code itself is ok, but I give @webId as parameter to call inner procedure which is called after code, and there was error. I had all rows with one webId, but exactly one row with another (exception in design) and I pass for this one row same webId as for another rows, and then in this subroutine error appeared. So error message was not for code above, but for some code in called procedure…

    Oh my…5 hours of time….

    In every way, thank you very much for trying help!
    I learned another thing about errors in T-SQL today.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from

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.