Need your help to figure out what is my mistakes using dynamic SQL Query. Below is my sample query, but I cannot detect where are my mistakes.
DECLARE @p_orgcompreq NVARCHAR(10)='1.1'
DECLARE @table_name NVARCHAR(50)='test'
DECLARE @treelevel INT
DECLARE @OrgCompCode_Parent NVARCHAR(10)='1.1'
DECLARE @OrgCompCode_Child NVARCHAR(10)='1.1.1'
DECLARE @SQLQuery NVARCHAR(max)
SELECT @treelevel = @@NESTLEVEL - 1
SET @SQLQuery = 'insert into ' + @table_name
+
' (level,OrgCompCode_Parent,OrgCompCode_Child) values (replicate(CHAR(45), '
+ Ltrim(Str(@treelevel))
+ ' * 1) + ltrim(str('
+ Ltrim(Str(@treelevel)) + ')),''' + ( @OrgCompCode_Parent ) +
''',''' +
@OrgCompCode_Child + ')'
EXEC (@SQLQuery)
SELECT @SQLQuery
And below are the raised error. Hope someone could help me. Thanks.
Msg 105, Level 15, State 1, Line 2 Unclosed quotation mark after the character string '1.1.1)'. Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '1.1.1)'.
(1 row(s) affected)