sql

TRUNCATE TABLES with CONSTRAINTS

Is there a way to truncate tables with constraints ? I tried to DEACTIvATE with this: DECLARE @SQLtxt varchar(max) SET @SQLtxt = '-- DESACTIVER LES CONTRAINTES' + CHAR(10) SELECT @SQLtxt = @SQLtxt + 'ALTER TABLE [' + name + '] NOCHECK CONSTRAINT ALL;' FROM sys.tables PRINT @SQLtxt Of course, it didn't worked. I have to drop the const...

Reporting Stored Procedure - How to avoid duplication?

I'm writing a reporting stored procedure. I want to get the number of non-Acknowledged and non-Invoiced Purchase Orders, with the ability to (optionally) filter on CustomerID. What I have below works as expected, but I worry that a) it's slow and b) there's duplication in the CustomerID portion of the WHERE clause. How would you write...

Hierarchical query in Oracle - out of control?

I have a hierarchical query in Oracle 10 SQL that used to work. However, I removed the materialized view it was based on, and now I can't get it to come out properly, even leaving that view out altogether. The original query looked like this: select oh.name, oh.description , sys_connect_by_path(groupname, ':') "Groups" , (select c...

search database table for value and put it in a datatable

I have a textbox and a search button i trying to search file names in a database table and display them in a datatable... private void GetSearchResults(string machineID, string searchFileName) { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(); connection.ConnectionString = ConfigurationManager....

Ranking within Django ORM or SQL?

Hi, so... I have a huge list ranked by various values (eg. scores) So I grab the list ordered by these values: players = Player.objects.order_by('-score', '-karma') I would like to: Grab a player and get the neighbouring players P1 score:123 P2 score:122 YOU! score:110 P3 score:90 P2 score:89 Grab the...

SQL Server 2005 stored proc execution

Using SQL Server 2005: I have one stored proc that calls several others within it. One code segment, as an example, goes: INSERT INTO Log (...) VALUES (A...) EXECUTE StoredProcA params... INSERT INTO Log (...) VALUES (A...) INSERT INTO Log (...) VALUES (B...) EXECUTE StoredProcB params... INSERT INTO Log (...) VALUES (B...) It appe...

SQL View. Select count... Where...

I am most confused with this one, so i d better ask the experts! These are the rows returned by a custom query of mine. Col1 Col2 Result Font Bold a Font Bold b Font Bold a Font Italic a Is there any way of using selecting count in the above (table) results in order getting this one? Col1 Col2 ResultA ResultB ...

How to identify last row on a insert statement

Hello, I want to know which record is the last inserted on Detail Table B in the following order... for example If I have : 1 row Header Table A -------------------- 1 row Detail Table B 2 row Detail Table B 3 row Detail Table B 4 row Detail Table B (last) I want to do some T-SQL or run a procedure if the 4 row is inserted... Thanks i...

changing the ID of an SSAS database

I want to rename my SALES cube to SALES_2009 and put a new cube called SALES. When I rename it, the ID remains as SALES so my new cube now is called SALES, but it's internal ID is SALES_2010 (and my old cube is called SALES_2009, but the internal ID is SALES). Can the ID of a SSAS database be changed? I have tried the properties windo...

SQL: How to select highest PK out of multiple records returned

Hello all, I have a Journal_Entry table, with a Primary Key of Journal_Entry_ID, and (among other columns) an Entry_Date column. I'm trying to do a query that selects the most recent Entry_Date -- via a SELECT MAX(Entry_Date) -- but the problem is that the user may have logged more than one entry on a given date. So if the user logged ...

Storing matrices in a relational database

Hey guys, I am working on a project for a client and going through the initial database design. The project will be a simple web app for tracking processes and their outcomes within a matrix diagram, I am looking for a good way to store these in relational tables. Right now I am thinking I have a general table for Routines which the x...

How can I Inner Join two tables in Access when one column is a text and the other is number?

I have a program in access that is using some linked ODBC tables. I had originally had a query that contained the following INNER JOIN: FROM Neptune_FN_Analysis INNER JOIN Neptune_prem ON Neptune_FN_Analysis.PremID = Neptune_prem.premice_id This worked just fine until the column Neptune_prem.premice_id got changed from a number to a...

SQL Join on Nearest less than date.

Normally I would just do this in the code itself, but I am curious if this can be accomplished efficiently in TSQL. Table 1 Date - Value Table 2 Date - Discount Table 1 contains entries for each day. Table 2 contains entries only when the discount changes. A discount applied to a value is considered valid until a new discount is en...

mysql Select related objects by tags

On a page which shows a specific object I need to display related objects for that object based on tags. ´The object with the most matching tags should be at the top. Basically I somhow need to identify how many tags for each object that match the object on the page and display the top results. My db schema: Table Object ------------ ...

Global variables in SQL

Let's say I wanted to create a sql script and do something like this: DECLARE @SomeVariable int SET @SomeVariable = 'VALUE' FROM someTable --do stuff with @SomeVariable GO CREATE PROCEDURE myProcedure ( @MyParameter ) AS SET NOCOUNT ON --Do something --Do something using @SomeVariable SET NOCOUNT OFF RETURN 0 GO I can't, because...

How to get number of specific rows from a different table in a subquery

I know it's possible, but I'm not experienced enough to know how to do subqueries. Here's the situation: Table 1: +--------------------+--------------------+ | v_id | v_name | +--------------------+--------------------+ | 1 | v_name1 | +--------------------+--------------------+ | et...

Closing SQL connections (NHibernate)

I have a test console app that is executing sql scripts to create a database and its tables, then inserting data. I use DAO to create, retrieve, update, and delete from the tables and then try and drop the database, but it can't because it says it is currently in use. How do I kill the connection? Through debugging and running a sql scri...

Java/Hibernate: how to write DAO code for complex SQLs

Hi all, My current workplace uses the standard Spring/Hibernate/JSP mix to serve content to its Flex client via XML. There are many ways in which the data is accessed, but the most prevalent one is via direct SQL calls to the database and a manual conversion to XML. The trouble is that as the application grew bigger, the SQLs became mu...

How to optimize or remove redundancy from following query

I have 4 tables Table1 (employee) id name -------------------- 1 a 2 b Table2 (appointment) id table1id table3id table4id sdate edate typeid ----------------------------------------------------------------------------------- 1 1 1 1 1/1/09 NULL 100...

Database to return multi-dimensional result

This is a follow-up question to my previous one. Situation: Table 1: +--------------------+--------------------+ | v_id | v_name | +--------------------+--------------------+ | 1 | v_name1 | +--------------------+--------------------+ | etc... Table 2: +--------------------+--------...