| Author |
Topic |
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-19 : 14:57:44
|
| Ok, I have a SQL 2005 DB. The DB was created by my issue and inventory tracking software that we use at my company. This software tracks customer info like address and phone, then we track hardware that we sell to said customers and it also tracks issues that are called in by customers.So what I need is a field in the DB that will generate either a random or sequential (and unique) number for every customer. I would prefer the number to be about 6-8 digits longWithin the software I can create "custome fields" which will display within the GUI of the software. It then creates a corrisponding column within whatever table I choose (like companies or issues or assets). So I have created a "Company_ID_Test" custom field within the Company table.That is the field I need to have popluate with the random or sequental number.Currently there is a column within the Company table called "CO_COMPANY_ID" which has sequental numbers, however that column is not displaied anywhere within the software. And the numbers have started at 1 so they are a little short for what I need. But if I could some how use that column and add 1000000 to the number that would be fine too. The software is obvisouly generating the sequential number already, and if we can use that column and just add more digits that would be ok too.I just need a longer and still unique number for each company in the table. I also need this number to populate to new companies that are added to the database from the application.Any thoughts?? |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-19 : 16:04:30
|
| After I noticed some articles on this site, I found the NEWID that could create a GUID. I think this would be great, but I can't seem to figure it out. I have an existing table so can I add a column to that table that will used the NEWID and create a GUID for existing and new companies in that table?? I am a TOTOAL NEWB with SQL so be kind. I'm using SSMS express to do my querys.Thanks!! |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-09-19 : 20:46:11
|
| Just create an identity column and start it at a higher number if you really must. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-21 : 02:46:36
|
quote: Originally posted by stewart05 After I noticed some articles on this site, I found the NEWID that could create a GUID. I think this would be great, but I can't seem to figure it out. I have an existing table so can I add a column to that table that will used the NEWID and create a GUID for existing and new companies in that table?? I am a TOTOAL NEWB with SQL so be kind. I'm using SSMS express to do my querys.Thanks!!
you can. but i think you may use NEWSEQUENTIALID() instead of NEWID(). something like belowALTER TABLE YourTableName ADD Company_guid NOT NULL DEFAULT NEWSEQUENTIALID() |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-21 : 22:53:30
|
| Can you tell me how to change an existing column wihtin my table to create the GUID? I have to use an already existing column because its already been created within the frontend application.Thanks!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 03:31:37
|
quote: Originally posted by stewart05 Can you tell me how to change an existing column wihtin my table to create the GUID? I have to use an already existing column because its already been created within the frontend application.Thanks!!!!
not sure you can alter existing column to be of GUID type. but you could do the following work around.1.Create a new GUID column using query provided earlier2.Drop any constraints,indexes etc existing on your current column after scripting them out from sql management studio3. rename the newly created column to your existed column name using sp_rename system stored procedure4. Reapply the constraints on the new column. |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-22 : 09:54:17
|
| Ok, so I sort of have this working, but I still have problems. The application I'm using is a "off the shelf" product I purchased so I don't have access to do any development to the frontend app at all (just and fyi). So I'm trying to do all this within the current appliction.So I tried using the above code and got an error about not having data type and ended up figuring out that this is the code I needed:ALTER TABLE dbo.COMPANIES ADD COMPANY_ID_TEST uniqueidentifier NOT NULL DEFAULT NEWSEQUENTIALID()So to get this to work within the application here is what I had to do.The application allows me to create "custom fields" that will display within the application, so within companies, for example I needed to track the WAN IP address of my customers so I created a custom field for IP Address that shows up under every company. When I create these custom fields I get some options that affect the column within the SQL database, like column type which has choices of "text, date/time, decimal, Whole number, boolean" Also column length, default value, and required (or null). Those are the only options that I have from the application to set the new column.So I created a custom field call "company_id_test" from the application. From SSMS I deleted that new column and then ran the query that recreated the same column with the guid settings. Then all my existing companies showed up with a new GUID in that column. My problem comes when I try to create a new company. When I create a new company, I don't put anything in the company_id_test field within the application and when I tell it to "create" the company I get an error saying I can't have a null value in that field. So I went into SSMS and set it to allow nulls, and when I create a new company I don't get that error anymore but I also don't get it to create a GUID for that new company...Anyone have any thoughts on how to have a GUID created for a new compnay I create??Thanks again for all the help guys!!!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 09:59:54
|
| But when you insert when you dont pass an explicit value for company_id_test field, it should put default value which is next sequential id and should not give any error. Are you sure you're not including column in insert list at all? |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-22 : 10:03:22
|
quote: Originally posted by visakh16 Are you sure you're not including column in insert list at all?
Forgive me, I am totally new to SQL and I don't understand what you're asking here. What do you mean? |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-22 : 10:24:20
|
| I did just notice something here. When I modified the company_id_test from not allowing nulls to allowing nulls, I got an error when I tried to save, and it said:"error validating the default for column 'company_id_test'.When I look at the "Default Value or Binding" for that column it has (newsequentialid()) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 10:55:03
|
quote: Originally posted by stewart05
quote: Originally posted by visakh16 Are you sure you're not including column in insert list at all?
Forgive me, I am totally new to SQL and I don't understand what you're asking here. What do you mean?
show your insert query |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-22 : 11:21:59
|
| ALTER TABLE dbo.COMPANIES ADD COMPANY_ID_TEST uniqueidentifier NOT NULL DEFAULT NEWSEQUENTIALID()is that my insert query??If you mean the query that runs when I add a company, I have no idea because that is all done via the frontend application. I do it all via a windows client software. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 11:26:06
|
quote: Originally posted by stewart05 ALTER TABLE dbo.COMPANIES ADD COMPANY_ID_TEST uniqueidentifier NOT NULL DEFAULT NEWSEQUENTIALID()is that my insert query??If you mean the query that runs when I add a company, I have no idea because that is all done via the frontend application. I do it all via a windows client software.
then i guess the problem is wit the query run by application. it seems like its trying to pass explicit NULL or blank value to guid field. |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-22 : 12:30:37
|
| Ok, so I just talked to the software developers support and the guy wasn't sure if they were sending an "explicit NULL or not, but thats sounding like the most likley answer here.SOOOO, my question is this now...Can mirror a column to another column?? Meaning, can I create a NEWSEQUENTIALID() column that is not affected by the insert query so it will create an actual guid for each company, but then create another column from within the app, and run a query that will basically mirror that column to the NEWSEQUENTIALID() column??Does this make sense?? |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-22 : 16:44:12
|
| Ok, so heres what I've now done.I have created a column that generates the GUID using the newsequentialid and it is NOT tied to the frontend application in anyway. So everytime I create a new entry into the table COMPANIES each company gets a new GUID in that column.The frontend app, has a field that displays in the app that I don't use, its column is CO_COMPANY_CODE. So I did this query:UPDATE COMPANIES SET CO_COMPANY_CODE = COMPANY_ID_TESTAnd it copied all the GUIDS from COMPANY_ID_TEST to CO_COMPANY_CODE and I can see each companies GUID from the application. But now I can't get it to update the CO_COMPANY_CODE column when I enter a new compnay from the app. Is this where I would need to use a stored procedure? Maybe somehthing that says "everytime a new entry in table COMPANIES occurs then do this (UPDATE COMPANIES SET CO_COMPANY_CODE = COMPANY_ID_TEST)"Is that possible??Thanks again for all the help, I'm SOOOOO close now!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-23 : 00:16:14
|
quote: Originally posted by stewart05 Ok, so heres what I've now done.I have created a column that generates the GUID using the newsequentialid and it is NOT tied to the frontend application in anyway. So everytime I create a new entry into the table COMPANIES each company gets a new GUID in that column.The frontend app, has a field that displays in the app that I don't use, its column is CO_COMPANY_CODE. So I did this query:UPDATE COMPANIES SET CO_COMPANY_CODE = COMPANY_ID_TESTAnd it copied all the GUIDS from COMPANY_ID_TEST to CO_COMPANY_CODE and I can see each companies GUID from the application. But now I can't get it to update the CO_COMPANY_CODE column when I enter a new compnay from the app. Is this where I would need to use a stored procedure? Maybe somehthing that says "everytime a new entry in table COMPANIES occurs then do this (UPDATE COMPANIES SET CO_COMPANY_CODE = COMPANY_ID_TEST)"Is that possible??Thanks again for all the help, I'm SOOOOO close now!!!!
then do this in a trigger. Write an insert trigger and do update inside it. |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-23 : 09:35:11
|
| I don't mean to be so needy, but is there anyway you could tell me how to do that. I am totally new to SQL and what I have done in this thread has been my full experience with SQL and really any database so I know next to nothing about this stuff.Thanks again!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-23 : 09:38:24
|
quote: Originally posted by stewart05 I don't mean to be so needy, but is there anyway you could tell me how to do that. I am totally new to SQL and what I have done in this thread has been my full experience with SQL and really any database so I know next to nothing about this stuff.Thanks again!!!
something likeCREATE TRIGGER UpdateGUIDON COMPANIESAFTER INSERTASUPDATE cSET c.CO_COMPANY_CODE = i.COMPANY_ID_TESTFROM COMPANIES cINNER JOIN INSERTED iON i.PKCol=c.PKCol where PKCol is primary key of your table. |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-23 : 10:24:37
|
| Hey, its me again...I tried this:CREATE TRIGGER UpdateGUIDON COMPANIESAFTER INSERTASUPDATE cSET c.CO_COMPANY_CODE = i.COMPANY_GUIDFROM COMPANIES cINNER JOIN INSERTED iON i.CO_COMPANY_ID=c.CO_COMPANY_IDI changed a few column names around like my GUID column is now COMPANY_GUID, and the PK of the Companies table is CO_COMPANY_IDBut this didn't work. When I created a new Company from the application, the COMPANY_GUID created the GUID, but it did not get copied over to the CO_COMPANY_CODE column.Any thoughts? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-23 : 11:32:55
|
quote: Originally posted by stewart05 Hey, its me again...I tried this:CREATE TRIGGER UpdateGUIDON COMPANIESAFTER INSERTASUPDATE cSET c.CO_COMPANY_CODE = i.COMPANY_GUIDFROM COMPANIES cINNER JOIN INSERTED iON i.CO_COMPANY_ID=c.CO_COMPANY_IDI changed a few column names around like my GUID column is now COMPANY_GUID, and the PK of the Companies table is CO_COMPANY_IDBut this didn't work. When I created a new Company from the application, the COMPANY_GUID created the GUID, but it did not get copied over to the CO_COMPANY_CODE column.Any thoughts?
are two columns of same type? CO_COMPANY_CODE & COMPANY_GUID |
 |
|
|
stewart05
Starting Member
15 Posts |
Posted - 2008-09-23 : 12:29:36
|
| No, CO_COMPANY_CODE is varchar 64 length and the COMPANY_GUID is the uniqueidentifier 16 lenght.I don't think I can change any properties of the CO_COMPANY_CODE without messing up my frontend application. |
 |
|
|
Next Page
|