I have a section of code...
TryUpdateModelAsync(tn.IT_Task, "IT_Task");
...that used to work reasonably speedy. Now, that same piece of code is achingly slow. Sometimes up to 10-15 seconds to complete.
Using...
DB.ITConsole.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
...to output what was happening in the background, I found that the method was opening and closing the database literally several 1000 times. Seemed to me to be at least as many times as there are "IT_Task" records. Perhaps more. Small sample of the output below...
What's going on? How can I get a single record on the model updated from the views collection?
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Project_Note_ID] AS [Project_Note_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Description] AS [Description]
FROM [dbo].[IT_Project_Note] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "164" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Task_ID] AS [Task_ID],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Reported_By_ID] AS [Reported_By_ID],
[Extent1].[NAB_ID] AS [NAB_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Description] AS [Description],
[Extent1].[Task_Category_ID] AS [Task_Category_ID],
[Extent1].[Task_Type_ID] AS [Task_Type_ID],
[Extent1].[Task_Status_ID] AS [Task_Status_ID],
[Extent1].[Task_Priority_ID] AS [Task_Priority_ID],
[Extent1].[Task_Severity_ID] AS [Task_Severity_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Date_Due] AS [Date_Due]
FROM [dbo].[IT_Task] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "164" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Project_Note_ID] AS [Project_Note_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Description] AS [Description]
FROM [dbo].[IT_Project_Note] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "203" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Task_ID] AS [Task_ID],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Reported_By_ID] AS [Reported_By_ID],
[Extent1].[NAB_ID] AS [NAB_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Description] AS [Description],
[Extent1].[Task_Category_ID] AS [Task_Category_ID],
[Extent1].[Task_Type_ID] AS [Task_Type_ID],
[Extent1].[Task_Status_ID] AS [Task_Status_ID],
[Extent1].[Task_Priority_ID] AS [Task_Priority_ID],
[Extent1].[Task_Severity_ID] AS [Task_Severity_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Date_Due] AS [Date_Due]
FROM [dbo].[IT_Task] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "203" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Task_Note_ID] AS [Task_Note_ID],
[Extent1].[Task_ID] AS [Task_ID],
[Extent1].[Description] AS [Description],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[CC_User] AS [CC_User]
FROM [dbo].[IT_Task_Note] AS [Extent1]
WHERE [Extent1].[Task_ID] = #EntityKeyValue1
-- EntityKeyValue1: "15669" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Project_Note_ID] AS [Project_Note_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Description] AS [Description]
FROM [dbo].[IT_Project_Note] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "235" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Task_ID] AS [Task_ID],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Reported_By_ID] AS [Reported_By_ID],
[Extent1].[NAB_ID] AS [NAB_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Description] AS [Description],
[Extent1].[Task_Category_ID] AS [Task_Category_ID],
[Extent1].[Task_Type_ID] AS [Task_Type_ID],
[Extent1].[Task_Status_ID] AS [Task_Status_ID],
[Extent1].[Task_Priority_ID] AS [Task_Priority_ID],
[Extent1].[Task_Severity_ID] AS [Task_Severity_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Date_Due] AS [Date_Due]
FROM [dbo].[IT_Task] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "235" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Project_Note_ID] AS [Project_Note_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Description] AS [Description]
FROM [dbo].[IT_Project_Note] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "243" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Task_ID] AS [Task_ID],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Reported_By_ID] AS [Reported_By_ID],
[Extent1].[NAB_ID] AS [NAB_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Description] AS [Description],
[Extent1].[Task_Category_ID] AS [Task_Category_ID],
[Extent1].[Task_Type_ID] AS [Task_Type_ID],
[Extent1].[Task_Status_ID] AS [Task_Status_ID],
[Extent1].[Task_Priority_ID] AS [Task_Priority_ID],
[Extent1].[Task_Severity_ID] AS [Task_Severity_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Date_Due] AS [Date_Due]
FROM [dbo].[IT_Task] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "243" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Project_Note_ID] AS [Project_Note_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Description] AS [Description]
FROM [dbo].[IT_Project_Note] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "257" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Task_ID] AS [Task_ID],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Reported_By_ID] AS [Reported_By_ID],
[Extent1].[NAB_ID] AS [NAB_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Description] AS [Description],
[Extent1].[Task_Category_ID] AS [Task_Category_ID],
[Extent1].[Task_Type_ID] AS [Task_Type_ID],
[Extent1].[Task_Status_ID] AS [Task_Status_ID],
[Extent1].[Task_Priority_ID] AS [Task_Priority_ID],
[Extent1].[Task_Severity_ID] AS [Task_Severity_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Date_Due] AS [Date_Due]
FROM [dbo].[IT_Task] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "257" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Project_Note_ID] AS [Project_Note_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Description] AS [Description]
FROM [dbo].[IT_Project_Note] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "285" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
Opened connection at 15/06/2017 16:59:40 +01:00
SELECT
[Extent1].[Task_ID] AS [Task_ID],
[Extent1].[Entered_By_ID] AS [Entered_By_ID],
[Extent1].[Reported_By_ID] AS [Reported_By_ID],
[Extent1].[NAB_ID] AS [NAB_ID],
[Extent1].[Project_ID] AS [Project_ID],
[Extent1].[Description] AS [Description],
[Extent1].[Task_Category_ID] AS [Task_Category_ID],
[Extent1].[Task_Type_ID] AS [Task_Type_ID],
[Extent1].[Task_Status_ID] AS [Task_Status_ID],
[Extent1].[Task_Priority_ID] AS [Task_Priority_ID],
[Extent1].[Task_Severity_ID] AS [Task_Severity_ID],
[Extent1].[Date_Entered] AS [Date_Entered],
[Extent1].[Date_Due] AS [Date_Due]
FROM [dbo].[IT_Task] AS [Extent1]
WHERE [Extent1].[Project_ID] = #EntityKeyValue1
-- EntityKeyValue1: "285" (Type = Int32, IsNullable = false)
-- Executing at 15/06/2017 16:59:40 +01:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 15/06/2017 16:59:40 +01:00
UPDATE - What am I trying to do with the TryUpdateModelAsync?
To my mind, my knowledge, TryUpdateModelAsync would only do one thing. Apologies if I am wrong.
I have created a model based on a particular record in the database along with some supporting material for dropdowns etc on that page, then sent it to the page for editing during the GET phase. Then during the POST phase I create another (same) model based on the same record.
The TryUpdateModelAsync is intended to take the data from the return POST and update the original record with that data, before I can then save it.
I really wasn't expecting the process to look in the database at all to merge these two sets of data. I would have only expected access to the database to take place upon the SaveChanges.
I was having the same problem where TryUpdateModelAsync was taking way too long to return. I figured that the problem was that it was trying to iterate through all the sub collections on my database object, and EF6 was lazy-loading everything along the way. This appeared to be the case because I got it to work by turning off lazy-loading before I got my object from the database by doing this:
_database.Configuration.LazyLoadingEnabled = false;
Related
I have a Windows-based production system which has started spawning 4 sessions every minute but leaving them in an IDLE state. While I'm trying to track down the cause of this issue, I want to create a scheduled task which shall call my function every hour or so to drop all IDLE sessions.
So far, I've written the following:
CREATE OR REPLACE FUNCTION drop_idle_session_connections()
RETURNS VOID AS $$
DECLARE
i_total INTEGER;
i_active INTEGER;
i_idle INTEGER;
retval BOOLEAN;
BEGIN
SELECT
(SELECT count(*) FROM pg_catalog.pg_stat_activity),
(SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE state = 'active'),
(SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE state IN ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled'))
INTO i_total, i_active, i_idle;
RAISE NOTICE 'DROPPING IDLE CONNECTIONS ...';
RAISE NOTICE ' BEFORE : Total: % Active: % Idle: %',
to_char(i_total, '9999'),
to_char(i_active, '9999'),
to_char(i_idle, '9999');
WITH inactive_connections AS (
SELECT
pid,
rank() over (partition by client_addr order by backend_start ASC) as rank
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid( )
AND
application_name !~ '(?:psql)|(?:pgAdmin.+)|(?:DataGrip.+)'
AND
datname = current_database()
AND
state IN ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled')
AND
current_timestamp - state_change > interval '5 minutes'
)
SELECT
pg_terminate_backend(pid)
INTO
retval
FROM
inactive_connections
WHERE
rank > 1;
SELECT
(SELECT count(*) FROM pg_catalog.pg_stat_activity),
(SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE state = 'active'),
(SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE state IN ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled'))
INTO i_total, i_active, i_idle;
RAISE NOTICE ' AFTER : Total: % Active: % Idle: %',
to_char(i_total, '9999'),
to_char(i_active, '9999'),
to_char(i_idle, '9999');
END;
$$ LANGUAGE plpgsql;
However, even if I SET ROLE postgres the main CTE doesn't close any connections at all.
What am I doing wrong here? If I run the CTE from psql, it works fine
I'm trying to get a 3D polygon using ST_MakeSolid() sfcgal function in PostGIS.
My table looks like this:
id_geom| xmin | xmax | ymin | ymax | zmin | zmax
--------+------+---------+---------+---------+-------+-------
1 | 12.3235 | 12.3239 | 45.4339 | 45.4341 | -7 | -14
2 | 12.3234 | 12.3238 | 45.4338 | 45.4339 | -2 | -7
I want another column with the geometry of the solid, but I can't figure out how to use vertex coordinates stored into my table instead of raw coordinates.
I read the question "ST_MakeSolid() creating an invalid solid from closed polyhedralsurfaceZ", and works:
SELECT ST_MakeSolid('POLYHEDRALSURFACE Z (
((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),
((0 0 1,1 0 1,1 1 1,0 1 1,0 0 1)),
((0 0 0,0 0 1,0 1 1,0 1 0,0 0 0)),
((0 1 0,0 1 1,1 1 1,1 1 0,0 1 0)),
((1 1 0,1 1 1,1 0 1,1 0 0,1 1 0)),
((1 0 0,1 0 1,0 0 1,0 0 0,1 0 0)))')
But I want use "xmin", "xmax", etc. instead of 12.3235, 12.3239 etc..
How can I do this?
Thanks to Jim Jones i solved the issue:
SELECT
ST_MakeSolid('POLYHEDRALSURFACE Z ('
'('||'('||xmin_ ||' '|| ymin_ ||' '||zmin_ ||','|| xmin_ ||' '|| ymax_ ||' '||zmin_ ||','|| xmax_ ||' '|| ymax_ ||' '||zmin_ ||','|| xmax_ ||' '|| ymin_ ||' '||zmin_ ||','|| xmin_ ||' '|| ymin_ ||' '||zmin_||')'||')'||','||
'('||'('||xmin_ ||' '|| ymin_ ||' '||zmax_ ||','|| xmax_ ||' '|| ymin_ ||' '||zmax_ ||','|| xmax_ ||' '|| ymax_ ||' '||zmax_ ||','|| xmin_ ||' '|| ymax_ ||' '||zmax_ ||','|| xmin_ ||' '|| ymin_ ||' '||zmax_||')'||')'||','||
'('||'('||xmin_ ||' '|| ymin_ ||' '||zmin_ ||','|| xmin_ ||' '|| ymin_ ||' '||zmax_ ||','|| xmin_ ||' '|| ymax_ ||' '||zmax_ ||','|| xmin_ ||' '|| ymax_ ||' '||zmin_ ||','|| xmin_ ||' '|| ymin_ ||' '||zmin_||')'||')'||','||
'('||'('||xmin_ ||' '|| ymax_ ||' '||zmin_ ||','|| xmin_ ||' '|| ymax_ ||' '||zmax_ ||','|| xmax_ ||' '|| ymax_ ||' '||zmax_ ||','|| xmax_ ||' '|| ymax_ ||' '||zmin_ ||','|| xmin_ ||' '|| ymax_ ||' '||zmin_||')'||')'||','||
'('||'('||xmax_ ||' '|| ymax_ ||' '||zmin_ ||','|| xmax_ ||' '|| ymax_ ||' '||zmax_ ||','|| xmax_ ||' '|| ymin_ ||' '||zmax_ ||','|| xmax_ ||' '|| ymin_ ||' '||zmin_ ||','|| xmax_ ||' '|| ymax_ ||' '||zmin_||')'||')'||','||
'('||'('||xmax_ ||' '|| ymin_ ||' '||zmin_ ||','|| xmax_ ||' '|| ymin_ ||' '||zmax_ ||','|| xmin_ ||' '|| ymin_ ||' '||zmax_ ||','|| xmin_ ||' '|| ymin_ ||' '||zmin_ ||','|| xmax_ ||' '|| ymin_ ||' '||zmin_||')'||')'||')'
)
FROM mytable
I am trying to create a query for an SSRS report. I need to get the last 3 months transactions from a linked server (to an iSeries).
So I have built a SQL variable that I want to execute with OpenQuery
declare #EarliestDate varchar(8), #SQL VARCHAR(200), #sDate varchar(8)
SET #EarliestDate=CAST(DATEPART(YEAR,DATEADD(m,-3, getdate())) AS VARCHAR(4))
+ RIGHT('00' + CAST(DATEPART(mm, DATEADD(m,-3, getdate())) AS varchar(2)), 2)+ '01'
SELECT #SQL= 'SELECT * FROM YEDB03P WHERE TCDAT >' + '''' + #EarliestDate +'''' + ' ORDER BY TCDAT DESC'
print #EarliestDate
print #SQL
select * from openquery(BOCTEST2,#SQL)
But it seems that openquery doesn't like using a variable
What's the correct way to do this?
OPENQUERY does not accept variables for its arguments.
But you can move whole OPENQUERY statement into your dynamic SQL:
declare #EarliestDate varchar(8), #SQL VARCHAR(MAX), #sDate varchar(8)
SET #EarliestDate=CAST(DATEPART(YEAR,DATEADD(m,-3, getdate())) AS VARCHAR(4))
+ RIGHT('00' + CAST(DATEPART(mm, DATEADD(m,-3, getdate())) AS varchar(2)), 2)+ '01'
SELECT #SQL= 'select * from openquery(BOCTEST2, ''SELECT * FROM YEDB03P WHERE TCDAT >' + '''''' + #EarliestDate +'''''' + ' ORDER BY TCDAT DESC'')'
print #EarliestDate
print #SQL
EXEC #SQL
with help from Oleksandr Kucher I managed to do what I want with the below code (I added char(39) to the query string to force the quotes):
declare #EarliestDate varchar(8), #SQL NVARCHAR(1000), #sDate varchar(8)
SET #EarliestDate= CAST(DATEPART(YEAR,DATEADD(m,-3, getdate())) AS VARCHAR(4))
+ RIGHT('00' + CAST(DATEPART(mm, DATEADD(m,-2, getdate())) AS varchar(2)), 2)+ '01'
SELECT #SQL= 'insert into DBO.YEDB03P select * from openquery(BOCTEST2, ''SELECT * FROM YEDB03P WHERE TCDAT >'+char(39)+CHAR(39) + #EarliestDate +char(39)+CHAR(39) + ' ORDER BY TCDAT DESC'')'
--print #SQL
/*
The prints as
insert into DBO.YEDB03P select * from openquery(BOCTEST2, 'SELECT * FROM YEDB03P WHERE TCDAT >''20141001'' ORDER BY TCDAT DESC')
*/
exec sp_executesql #SQL
select * from PION..YEDB03P
thanks
I'm wirting a tsql procedure to recreate all the views in one database onto a different dtabase. I'm receiving the error "Incorrect syntax near +" When I look at the code though the syntax appears correct to me. Can anyone tell me what is wrong with this query?
USE [SOURCEDB]
BEGIN
DECLARE #SQL NVARCHAR(MAX)
DECLARE CUR_V CURSOR FOR
SELECT sc.text
FROM sys.views av
JOIN sys.syscomments sc ON sc.id = av.object_id
OPEN CUR_V
FETCH NEXT FROM CUR_V INTO #SQL
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC sp_executesql N'USE [DESTINATIONDB] EXEC sp_executesql ' + #SQL + ''
FETCH NEXT FROM CUR_V INTO #SQL END CLOSE CUR_V DEALLOCATE CUR_V END
Thanks in advance
You can't do the + in the same line as sp_executesql
declare #sql nvarchar(1000)
select #sql = N'USE [DESTINATIONDB] EXEC sp_executesql ' + #SQL + ''
exec sp_executesql #sql
or possibly what you mean is
select #sql = N'USE [DESTINATIONDB] EXEC sp_executesql N''' + #SQL + ''''
GO
SET NOCOUNT ON;
DECLARE
#idAdvertisements int,
#Name nvarchar(255),
#Description nvarchar(500),
#DepartureDate datetime,
#Cities_idCities int,
#Areas_idAreas int,
#Countries_idCountries int,
#Agencies_idAgencies int,
#Url nvarchar(1000),
#Price decimal(6, 2),
#HollidayDuration int,
#BookingDate datetime;
DECLARE ad_cursor CURSOR
FOR SELECT idAdvertisements
,Name
,Description
,DepartureDate
,Cities_idCities
,Areas_idAreas
,Countries_idCountries
,Agencies_idAgencies
,Url
,Price
,HollidayDuration
,BookingDate
FROM Advertisements;
OPEN ad_cursor;
FETCH NEXT FROM ad_cursor
INTO #idAdvertisements
,#Name
,#Description
,#DepartureDate
,#Cities_idCities
,#Areas_idAreas
,#Countries_idCountries
,#Agencies_idAgencies
,#Url
,#Price
,#HollidayDuration
,#BookingDate;
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT ' ';
PRINT #idAdvertisements;
--PRINT #Name;
--PRINT #Description;
--PRINT #DepartureDate;
--PRINT #Cities_idCities;
END
CLOSE ad_cursor;
But I always get 1, 1, 1, 1, 1. Data are always the same.
SELECT statement is OK. I don't understand why. Can someone see the problem?
cursors are Evil.. eVIL.. eVil.. stay away from them
Now, here is the issue - you are not moving the cursor forward..
DECLARE ad_cursor CURSOR
FOR SELECT idAdvertisements, Name, Description, DepartureDate, Cities_idCities, Areas_idAreas,
Countries_idCountries, Agencies_idAgencies, Url, Price, HollidayDuration, BookingDate FROM Advertisements;
OPEN ad_cursor;
FETCH NEXT FROM ad_cursor
INTO #idAdvertisements, #Name, #Description, #DepartureDate, #Cities_idCities, #Areas_idAreas,
#Countries_idCountries, #Agencies_idAgencies, #Url, #Price, #HollidayDuration, #BookingDate;
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT ' ';
PRINT #idAdvertisements;
--PRINT #Name;
--PRINT #Description;
--PRINT #DepartureDate;
--PRINT #Cities_idCities;
FETCH NEXT FROM ad_cursor
INTO #idAdvertisements, #Name, #Description, #DepartureDate, #Cities_idCities, #Areas_idAreas,
#Countries_idCountries, #Agencies_idAgencies, #Url, #Price, #HollidayDuration, #BookingDate;
END
CLOSE ad_cursor;
The code as you have it here will loop infinitely. You need another FETCH at the end of the body of the WHILE loop, otherwise ##FETCH_STATUS will never change.
OPEN ad_cursor;
FETCH NEXT FROM ad_cursor
INTO #idAdvertisements, #Name, #Description, #DepartureDate, #Cities_idCities, #Areas_idAreas,
#Countries_idCountries, #Agencies_idAgencies, #Url, #Price, #HollidayDuration, #BookingDate;
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT ' ';
PRINT #idAdvertisements;
--PRINT #Name;
--PRINT #Description;
--PRINT #DepartureDate;
--PRINT #Cities_idCities;
FETCH NEXT FROM ad_cursor
INTO #idAdvertisements, #Name, #Description, #DepartureDate, #Cities_idCities, #Areas_idAreas,
#Countries_idCountries, #Agencies_idAgencies, #Url, #Price, #HollidayDuration, #BookingDate;
END
CLOSE ad_cursor;
You have add fetch the next row, just before the END