sql

Dynamic sql statement to update a variable

Hi, my sql statement is something like this below DECLARE @OLD_NAV_VALUE AS INT DECLARE @FINAL AS INT SELECT @OLD_NAV_VALUE = [col1] from TBL_BA where DATE = @id_Date SET @FINAL = @OLD_NAV_VALUE * 50 But the problem i am haveing here is that the column name in the select statement which is given as [col1] is a dynamic value. So i a...

Cross Database Stored Procedure performance considerations

In my project I have two separate DBs on the same server. The DBs are self-sufficient except for three columns in DB "B" that need to be accessed in DB "A". Are there performance considerations if I were to have a stored proc in A that accessed three columns from B directly? Currently, we run a nightly job to import the needed data fro...

Is it a bad idea to use GUIDs as primary keys in MS SQL?

We have a system that uses UniqueIdentifier as the primary key of each of the tables. It has been brought to our attention that this is a bad idea. I have seen similar post on the subject but I am interested in any MS SQL performance and other potential problems I may encounter due to this decision. ...

SQL Update woes in MS Access - Operation must use an updateable query

I have a select query which does some text manipulation to essentially reformat a field so that I can look it up in another table: If my first table if I have a field like "J1/2" it looks up the ID of a record in a different table with J1 and J2 in the appropriate fields. This all works well. Now I want to update the original table so...

How do you deal with char(1) in place of a boolean and tri-state fields?

Somewhat related to my question about integers instead of decimals; my vendor provides a lot of "boolean" fields in the char(1) format (i.e. Y/N). Some of these fields legitimately cannot be a boolean because they can have several values, but the majority can be treated as a boolean. In my previous question the advice was to do what's ...

SQL Select Convention

Hi, We've encountered the following "issue". I like to use the following writing: SELECT Id, Name FROM Table1 JOIN Table2 ON Table1.FK1 = Table2.FK1 but a colleague of mine uses: SELECT Id, Name FROM Table1 JOIN Table2 ON Table1.FK1 = Table2.FK1 Is there a best practice for this? I think it's more convenient if all u...

How to select distinct elements when they appear repeated in a table? (T-SQL)

Consider the following table: create table temp ( name int, a int, b int ) insert into temp (name, a, b) values (1, 2, 3) insert into temp (name, a, b) values (1, 4, 5) insert into temp (name, a, b) values (2, 6, 7) I want to select *(all fields) with distinct [name]. In the case of two or more rows having the ...

Copy data and keeping referencial integrity for new IDs

I need a tool or method that allows the transfer of data and automatically updates the foreign keys at the destination table. The SET IDENTITY_INSERT ON/OFF is not what I'm looking for. Example: table master (id int identity, name char) table slave (id int identity, master_id int, name char) I would like to create a script like thi...

What is a good .NET library for deconstructing SQL queries that allows me to determine what table each column originates from?

My company needs to allow the internal users of our application to write queries directly against the database through our UI. But when we display the results, our code needs to know which column and table each return column is for so that the results can be displayed with some color coding and other logic for each column. So I was hopi...

Why won't mysql_real_escape_string() work?

I'm trying to write someting into the DB using PHP but if I try to use mysql_real_escape_string() I don't get any errors but nothing gets saved into the DB and I don't know why =/ specially because I did the same on another PHP file and it worked perfectly... <?php if(isset($_POST['reporte'])) $falla = $_POST['reporte']; else ...

T-SQL to trim a datetime to the nearest date?

Duplicate of http://stackoverflow.com/questions/2775/whats-the-best-way-to-remove-the-time-portion-of-a-datetime-value-sql-server I have a column that tracks when things are created using a datetime, but I'd like to generate a report that groups then by day, so I need a way of nulling out the time component of a datetime column. How do...

SQL Server Reporting Services Pricing

I am trying to understand the pricing requirements for a client to deploy reporting services reports to the web. Do they have to buy any sort of special Report Server license in order to deploy reports, or is that included in the standard versions of SQL 2005? ...

SQL Statement to Pull a List Using Data from Multiple Tables

Thanks for the response. Here's the problem....the Oildatasetstatusid's (5-11) are mapped to labid=4. Labid=4 has input data for multiple customers, so the update cannot be performed based on oildatasetstatusid. The updates have to be based on samplepointid. That's why 3 tables are being used. I am among the SQL ignorant and have been a...

How to compare string data to table data in SQL Server - I need to know if a value in a string doesn't exist in a column

I have two tables, one an import table, the other a FK constraint on the table the import table will eventually be put into. In the import table a user can provide a list of semicolon separated values that correspond to values in the 2nd table. So we're looking at something like this: TABLE 1 ID | Column1 1 | A; B; C; D TABLE 2 ID ...

how to create xml from xml fragments

I am using SQL Server 2005 and I have a XML Data Type column that stores xml fragments like so in one big table: row 1 ..... <Order date='2009-02-11' customerID='4' /> row 2...... <OrderItem OrderID='6' ItemID='477' quantity='1' /> I would like to create an XML using T-SQL that looks like this from these nodes: <Orders> <Order data...

Updating multiple rows with a value calculated from another column

I have a table with a row that looks like this: (2009123148498429, '...', '...') The first part, id, is a timestamp followed by a random number. (needed to work with other parts in the system) The data already exists in the table. I want to create a column, timestamp, and extract just the date (20091231) and update all the rows with ...

Want to display the current date/time

Hi I'm using php and sql through odbc to write a program and i hav got abit stuck in a part where i want to display the current date/time in the format date('Y-m-d H:i:s) but it only displays the gmt time. I want to add 8hours to it.Can any of you b able to help me.Thank you so much ...

Fix for 'return' in cakePHP to return Value from view controller funtion

Hi there I have a function inside of a view function inside of a model class in the model.php file that looks like this function sqlToUnix($date){ $YMDThenHMS = explode(" ", $date); $YMD = explode("-", $YMDThenHMS[0]); $HMS = explode(":", $YMDThenHMS[1]); $UnixTime = mktime($HMS[0], $HMS[1], $HMS[2], $YMD[1], $YMD[2], $...

SQL: Select records belonging to excluded category that ONLY belong to excluded category

I have a SELECT statement that works, and runs fast enough on my tables (<0.01sec on 50k+ products, 3k+ categories). But in my mind it's not very elegant and would like to hear any suggestions on making it better. There are 3 tables of interest: products - key productID categories - key categoryID products_tree - link table (...

SQL Server 2005 date comparison - collation issues

In my DB, I have a table that was created from an Excel sheet, via a Linked Server option. I am now trying to compare its contents versus one of my main tables. The tables I am comparing are in the same database. There is a date column in both tables, both types are datetime and have Collation of SQL_Latin1_General_CP1_CI_AS, the s...