travisbrown
Technical User
- Dec 31, 2001
- 1,016
Trying to figure out what I'm doing wrong here. About half way down I'm trying to get the SCOPE_IDENTITY() from the last insert. No error, but won't run any line referencing the SCOPE_IDENTITY var.
This is simply a one-off process, so don't look at as an eventual sproc.
This is simply a one-off process, so don't look at as an eventual sproc.
Code:
DECLARE @standard_name NVARCHAR(12);
DECLARE @standard_id NVARCHAR(3);
DECLARE @sSQL NVARCHAR(MAX);
DECLARE @counter AS INT;
DECLARE @totalrows AS INT;
DECLARE @last_parameter_id AS NVARCHAR(10);
SET @counter = 1;
SELECT @totalrows = COUNT(*) FROM [dbo].[standards];
BEGIN TRAN A;
PRINT 'Create Temp table for counting rows';
CREATE TABLE #tempstandards(standard_id INT, standard_group_id INT, standard_name NVARCHAR(12), medium INT, RowID INT);
INSERT INTO #tempstandards
SELECT standard_id, standard_group_id, standard_name, medium, ROW_NUMBER() OVER(ORDER BY standard_id) FROM [dbo].[standards];
WHILE (@counter <= @totalrows)
BEGIN
SELECT @standard_name = standard_name FROM #tempstandards WHERE RowID = @counter;
SELECT @standard_id = standard_id FROM #tempstandards WHERE RowID = @counter;
SET @sSQL = 'INSERT INTO [dbo].[parameter_limits]
SELECT b.parameter_id, [' + @standard_name + ']
FROM [keystone_standards].[dbo].[CSR4510soil] A
INNER JOIN dbo.parameters B ON B.parameter_name = A.parameter_id WHERE [' + @standard_name + '] IS NOT NULL';
PRINT 'Inserting parameter_limits for ' + @standard_name ;
EXEC(@sSQL);
-- BEGIN FAIL but no error message.
SELECT @last_parameter_id = SCOPE_IDENTITY();
PRINT 'Last parameter_limit_id:' + @last_parameter_id
-- won't PRINT, won't err.
SET @sSQL = 'INSERT INTO [dbo].[standard_parameter_limits]
SELECT ' + @standard_id + ', parameter_limit_id FROM [dbo].[parameter_limits] WHERE last_parameter_id > ' + @last_parameter_id;
-- If I comment out ' + @last_parameter_id; the dynamic query will run below. If not, no error, no PRINT.
-- END FAIL
PRINT 'Inserting parameter limit index for ' + @standard_id;
EXEC(@sSQL);
--SELECT * FROM [dbo].[standard_parameter_limits]
SET @counter = @counter + 1
END
DROP TABLE #tempstandards
ROLLBACK TRAN A