The code i have posted is an example to give the understanding of the queries. In the actual project there is some complex logic in forming the dynamic query.
I assume that you are using a cursor so that you can get the value of the identity column [dbo].[StockTransferNote].[WebSTNNo], so that it can be put into [dbo].[StnXItems].[WebSTNNo]?