Showing posts with label names. Show all posts
Showing posts with label names. Show all posts

Wednesday, March 7, 2012

inserting values for alias names within tables?

hi every1

I have a database table in which one of the fields is an alias for the identity field. That is the alias field self references the table and takes its value for the "id" field of tht table
as follows:
system_id int
system_name varchar(20)
sys_alias int
sys_pro varchar(80)
sys_values varchar(80)
so here the sys_alias takes wtever value the system assigns to the id field, system_id while inserting values into the table..
now my problem is i have to insert records into ths table frm aother tables using insert into...select from statements bt since the id values r enerated by the comp during value insertion i dunno how to give values fr the alias?

e.g insert into my_table(system_name,sys_alias,sys_pro,sys_values)
select x,(how to give ths field),y,z
from another_table
where...

neone who understood my problem and can help puhllezz post me a reply asap!
thnx:)

shuchiSounds like you'd have to do this in a trigger.|||what is the expression for the "alias" column? is it exactly the same value, or is it something like a character prefix plus the value?

perhaps you might consider using a view instead

rudy|||hi...
its exactly the same value..jus like a copy column fr the identity column..
i tried using max(syb_identity) and @.@.identity functions but they do not work specially since i put them in a sub query so theyre not calculated recursivelye but just once and insert the same identity value for all other inserted records!
can anyone help me what sortof view i shud create?
i am doing ths whole process thru a perl script so whatever sql tht needs to be done is done thru an sql file run from my perl script. mebbe asome sorta trigger will work tht automatically inserts the newest value of identityt generated into the alias field while insertion of records?
gosh i really need help here...so any ideas wud be gr8:)
thnx fr all the suggestions:)

-shuchi|||if this "alias" column is to have exactly the same values, then you don't really need it

just select it twice in any query -- select system_id
, system_name
, system_id as sys_alias
, sys_pro
, sys_values
from yourtable

rudy|||thanx a ton fr all the help...managed it:)

-shuchi

Friday, February 24, 2012

Inserting row with single quote

I'm inserting a row of people's names and addresses to a table. This seems to work great, unless the person has ' in their name, for example O' Riordan. When the address has single quote (such as Wilder's Path), I'd get the same error, unclosed quote when inserting the row.

What would be the best way to work around this, other than read the line and replace it with a space.

In a literal you need to double embedded quotes, so it'd be 'O''Riordan'.

The best solution is to use a parameterized query, then you don't have to worry about embedded quotes.

|||How to use parameterized query? the program i currently using are java.. can show me some examples on how to use parameterized query. ^-^|||

Parameterized query is very useful but you can also use string concatenation. I've written a sample on my blog on how to insert records with single quotes. Here's a sample TSQL script

PRINT 'This is the man''' + 's choice'

In order to do this, you use a two-single quote approach. Notice the use of a concatenation operator to separate the first part of the sentence and the one that comes after the single-quote and the use of a two-single quote approach. This simpy means that every time you have a single-quote character included in your string, you have to do string concatenation to accomodate the insertion of a single-qoute in your string.

|||

Take a look at these links

http://msdn2.microsoft.com/en-us/library/ms378878.aspx

http://msdn2.microsoft.com/en-us/library/ms378138.aspx

Hope this helps

|||Sorry.. after looking thru the website i still dun know how cause my sql statement is like that..

String query = "INSERT INTO addresses (" +
"company, address, name, hp, " +
"phone, fax, email, start, day, month, year, " +
"mrc, isp, des, sale, mark" +
") VALUES ('" +
fields.company.getText() + "', '" +
fields.address.getText() + "', '" +
fields.name.getText() + "', '" +
fields.hp.getText() + "', '" +
fields.phone.getText() + "', '" +
fields.fax.getText() + "', '" +
fields.email.getText() + "', '" +
fields.start.getText() + "', " +
Integer.valueOf(dd) + ", " +
Integer.valueOf(mm) + ", " +
Integer.valueOf(yy) + ", '" +
fields.mrc.getText() + "', '" +
fields.isp.getText() + "', '" +
fields.des.getText() + "', '" +
fields.sale.getText() + "', '" +
fields.mark.getText() + "')";

I may need to type S'pore in the fields.address.getText() but instead of ' i need to type " which is very troublesome... in this way how should i replace the " to ' ?

|||If you're not comfortable with this approach, create a function in your application that replaces the single quote to double quote. The sure-st way there is is to create a parameterized query

Inserting row with single quote

I'm inserting a row of people's names and addresses to a table. This seems to work great, unless the person has ' in their name, for example O' Riordan. When the address has single quote (such as Wilder's Path), I'd get the same error, unclosed quote when inserting the row.

What would be the best way to work around this, other than read the line and replace it with a space.

In a literal you need to double embedded quotes, so it'd be 'O''Riordan'.

The best solution is to use a parameterized query, then you don't have to worry about embedded quotes.

|||How to use parameterized query? the program i currently using are java.. can show me some examples on how to use parameterized query. ^-^|||

Parameterized query is very useful but you can also use string concatenation. I've written a sample on my blog on how to insert records with single quotes. Here's a sample TSQL script

PRINT 'This is the man''' + 's choice'

In order to do this, you use a two-single quote approach. Notice the use of a concatenation operator to separate the first part of the sentence and the one that comes after the single-quote and the use of a two-single quote approach. This simpy means that every time you have a single-quote character included in your string, you have to do string concatenation to accomodate the insertion of a single-qoute in your string.

|||

Take a look at these links

http://msdn2.microsoft.com/en-us/library/ms378878.aspx

http://msdn2.microsoft.com/en-us/library/ms378138.aspx

Hope this helps

|||Sorry.. after looking thru the website i still dun know how cause my sql statement is like that..

String query = "INSERT INTO addresses (" +
"company, address, name, hp, " +
"phone, fax, email, start, day, month, year, " +
"mrc, isp, des, sale, mark" +
") VALUES ('" +
fields.company.getText() + "', '" +
fields.address.getText() + "', '" +
fields.name.getText() + "', '" +
fields.hp.getText() + "', '" +
fields.phone.getText() + "', '" +
fields.fax.getText() + "', '" +
fields.email.getText() + "', '" +
fields.start.getText() + "', " +
Integer.valueOf(dd) + ", " +
Integer.valueOf(mm) + ", " +
Integer.valueOf(yy) + ", '" +
fields.mrc.getText() + "', '" +
fields.isp.getText() + "', '" +
fields.des.getText() + "', '" +
fields.sale.getText() + "', '" +
fields.mark.getText() + "')";

I may need to type S'pore in the fields.address.getText() but instead of ' i need to type " which is very troublesome... in this way how should i replace the " to ' ?

|||If you're not comfortable with this approach, create a function in your application that replaces the single quote to double quote. The sure-st way there is is to create a parameterized query