Questions tagged [with-statement]

A number of languages have With statements. The Python with statement creates a new context, with associated context manager. When the context (a code block) is exited again, the context manager is notified. Please use "common-table-expression" for the SQL WITH construction.

A number of languages have With statements. Python's with statement creates a runtime context, defined by a . As the code block under the with statement is entered, a __enter__ hook is called on the context manager, and as it is exited (by any means, including exceptions and return statements), the __exit__ hook is called.

Python provides several standard context managers. File objects, for example, can be opened as a context manager, and on exit the file is automatically closed.

Context managers were defined in PEP 343.

Please use for the SQL WITH statement

1119 questions
0
votes
1 answer

postgres WITH query claims no FROM-clause

I've written a simple query that uses a WITH clause, but I'm getting this error: Error : ERROR: missing FROM-clause entry for table "cte" Here's the query, where I'm clearly putting a FROM clause. I know this must be simple but I'm just not…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
0
votes
3 answers

Use with statement in a class that wraps a resource

If I have a class that wraps a resource, e.g., an sqlite database connection or a file, is there a way I can use the with statement to close the resource when my object goes out of scope or is gcollected? To clarify what I mean, I want to avoid…
Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
0
votes
5 answers

How can I conditionally execute code in a "with" block?

I guess this is kinda abusing the feature, but I'm still curious whether it could be done - I want to do something like: with conditional(a): print 1 so that the print 1 part is executed only if a==True. Is this possible? EDIT: Like people…
olamundo
  • 23,991
  • 34
  • 108
  • 149
0
votes
1 answer

Reading a list from a file

I want to read from a .txt file, which has data saved as in list format. ['http://www.example.com/?date=20080301', 'http://www.example.com/?date=20080302', 'http://www.example.com/?date=20080303'] I have the following code: with open("test.txt",…
nutship
  • 4,624
  • 13
  • 47
  • 64
0
votes
1 answer

Why with() in R is doing vector operation in one case and not in the other?

I have the following table : > head(datalist[[5]]) X5CO X5CS X5CD X5CSD 1 24.87769 24.31233 26.84647 34.3316 2 24.74026 24.31233 26.84647 34.3316 3 24.45217 24.31233 26.84647 34.3316 10 24.87769 24.31233 26.15139 34.3316 11…
Wicelo
  • 2,358
  • 2
  • 28
  • 44
0
votes
1 answer

How to get with statement working with zinnia tag

I got a problem with iteration on zinnia tag outcome. Let's say that that tag returns a list of some categories, I tried to manage it in few ways: {% with categories=get_plain_categories %} {% for category in categories %}

{{…

mdargacz
  • 1,267
  • 18
  • 32
0
votes
4 answers

SQL WITH clause doesn't work

I'm trying to execute seemingly simple request contains WITH clause: WITH sub AS (SELECT url FROM site WHERE id = 15) SELECT * FROM search_result WHERE url = sub.url But it doesn't work. I get ERROR: missing FROM-clause entry for table…
Alex
  • 1,147
  • 1
  • 9
  • 17
0
votes
2 answers

postgresql WITH statement add empty records to report

I'm writing a request for postgresql database (9.2) to describe the structure of all schema tables. To make the result of the request more readable, I want to add an epmty record after rows, corresponded to the exact table. I think the easiest way…
xacinay
  • 881
  • 1
  • 11
  • 29
0
votes
2 answers

oracle sql developer: identify fields in table that have changed over time

Oracle version: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod My table structure: Field 1: Name Field 2: Year Field 2: Sales_Rev Field 3: Rep_Cat There are three types of Rep_Cat: Inside, Outside, Lead I need to identify…
lostinthebits
  • 661
  • 2
  • 11
  • 24
0
votes
2 answers

SQL WITH in WITH

I want to write a with in with SQL query. Go some error. Please help: DECLARE @Start AS DATETIME; DECLARE @End AS DATETIME; SET @Start = '2013-04-09'; SET @End = '2013-04-11'; with View_Solidnet_Training as ( with View_Solidnet_Training as ( select…
user2206834
  • 379
  • 1
  • 5
  • 13
0
votes
1 answer

CTE looping query

First I'll explain you the situation. I want to transfer data from one table(View_Solidnet_Training) to another(OBJ_Availability). There is one problem: In the view there is a Start- and EndDate! In OBJ_Availability every date has one record. So one…
user2206834
  • 379
  • 1
  • 5
  • 13
0
votes
1 answer

Adapting With/CTE statement

Using SQL Server 2012.... This code works as a standalone script but I need the value returned within a iif(...) nested in a Case...when as part of a larger script: with GroupedValues (Value, Frequency, SelectingCriteria) AS (Select Table1.Cost…
0
votes
0 answers

with as and premature file closure

I have a python function that contains the following code: with open(modelfilepath, "rb") as modelfile, open(vcffilepath, "rb") as vcffile: for row in gtf_getrow(modelfile): print row #add features as appropriate if…
Eicos
  • 35
  • 7
0
votes
2 answers

with open inside try - except block, too many files open?

Quite simply, I am cycling through all sub folders in a specific location, and collecting a few numbers from three different files. def GrepData(): import glob as glob import os as os os.chdir('RUNS') RUNSDir = os.getcwd() Directories =…
Daniel
  • 385
  • 1
  • 4
  • 12
0
votes
1 answer

DELETE rows with foreign constraints without using "on cascade"

I am a newb to SQL since I can normally get away with simple queries/migrations without digging into it, however I have a more complex one that doesn't involve a join and I am wondering how I delete when there is a foreign constraint without using…
LLL
  • 1,085
  • 8
  • 16