|
Hi,
I'm in need of a stored procedure which takes 5 input values and then stores them in 'Table A'. This is simple enough.
However i also need to get data from a 2nd table 'Table B', return 5 random records(1 column only) and then input these records into 'Table A' along with the other data mentioned above. (Making a single record / row of 10 bits of information)
Hopefully this makes sense.
I know how to create the select statement for the above, but how do i then capture this data within the stored procedure and use it in the insert statement?
Thanks!
|
|
|
|
|
Your question is not very clear, because
a) You never specified if the data in TableA has been inserted already and
then you want to update the 6th column of TableA [ As you
specified that the first 5 fields of TableA will have values supplied from
the Stored Proc which indicates that the 6th column will have values from
TableB ] with the values from TableB.
b) The Table Structure of both the tables are absent.
c) What will be the ultimate output [ Any rough sketch will make the
picture more clear]
d) Whether Table2's column will have predefined value or not. If not
whether the values needs to be inserted at runtime in TableB and then it
has to be fetched and next to be inserted in TableA?
e) Any dependency is lying between the two tables(e.g. Referential
Integrity Constraint)
However, I am assuming that TableA has the following structure
TableA
Col1 Col2 Col3 Col4 Col5 Col6
All are Varchar(50) types.
N.B.~ Col6 will have the value from TableB
TableB
RandomCol
Again Varchar(50)
And my Stored Proc is as under:
ALTER PROCEDURE InsertRecordsIntoTableA
-- Add the parameters for the stored procedure here
(
@VarCol1 AS VARCHAR(50),
@VarCol2 AS VARCHAR(50),
@VarCol3 AS VARCHAR(50),
@VarCol4 AS VARCHAR(50),
@VarCol5 AS VARCHAR(50)
)
AS
BEGIN
-- VARIABLE DECLARATION
DECLARE @RANDOMCOLUMNVALUES VARCHAR(50)
-- STEP 1: DECLARE A CURSOR
DECLARE MYRANDOMCURSOR CURSOR FOR
SELECT RANDOMCOL
FROM TABLEB
-- STEP 2: OPEN THE CURSOR
OPEN MYRANDOMCURSOR
FETCH MYRANDOMCURSOR INTO @RANDOMCOLUMNVALUES
-- STEP 3: START THE LOGIC
WHILE @@Fetch_Status = 0
BEGIN
-- STEP 4: INSERT THE COMBINED RECORDS INTO TABLE A
INSERT INTO TABLEA(COL1,COL2,COL3,COL4,COL5,COL6)
VALUES(@VarCol1,@VarCol2,@VarCol3,@VarCol4,@VarCol5,@RANDOMCOLUMNVALUES)
-- STEP 5: GET THE NEXT RECORD
FETCH MYRANDOMCURSOR INTO @RANDOMCOLUMNVALUES
END
--STEP 6: CLOSE THE CURSOR
CLOSE MYRANDOMCURSOR
--STEP 6: DEALLOCATE THE CURSOR
DEALLOCATE MYRANDOMCURSOR
END
Hope this answers you question.
If not, please tell your problem precisely with necessary
description
It can even be solved without using CURSOR and with a while loop
Please be specific about your question so that others can understand
properly.
Niladri Biswas
|
|
|
|
|
Good Day all i have the Following Query
select distinct convert(varchar(10), (convert(smalldatetime, c.descr)), 120) [Date], tt.Sess [Session], m.descr [Subject], ctyp.Abrev [PaperType], ct.Number [PaperNumber], a.GrpName [Campus], a.Duration, CASE WHEN MAV.STUDENTS IS NULL THEN a.Students ELSE MAV.STUDENTS END [Students], v.descr [Venue], v.Capacity, s.descr [Staff]
-- into #Timetable
from sol_actv_time tt
inner join tbl_clmn c on c.id=tt.Dy
inner join tbl_actv a on a.id = tt.actv
inner join tbl_cntc ct on ct.id = a.cntcid
inner join tbl_cntc_typ ctyp on ctyp.id = ct.cntctyp
inner join tbl_modl m on m.id = ct.modlid
inner join tbl_term t on t.id = a.term
left outer join sol_actv_venu ttVenu on ttVenu.actv = a.id
left outer join mtm_actv_venue mav on mav.venue=ttVenu.Venu and mav.actv=a.id
left outer join tbl_venue v on v.id = ttVenu.Venu
left outer join sol_actv_staff ttStaff on ttStaff.Actv = a.id
left outer join tbl_staff s on s.id = ttStaff.Staff
select activity.* from #Timetable activity for xml auto, elements
I get the Following Error
Msg 295, Level 16, State 3, Line 2
Conversion failed when converting character string to smalldatetime data type.
Thanks
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
I don't have sql server in front of me so I can't test unfortunately.
What is the data type of c.descr?
|
|
|
|
|
Its a String but its in a Date Format.
Thanks i have resolved this by accepting the data as data from my presentation layer.
Thanks
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
Hi All
I'm not a SQL guy, so I'm stuggling a bit. I hope you guys can help me.
I have an SSIS package which copies tables from one server to another. However, the source tables will, from time to time, get new fields added to it. My understanding of SSIS is that when you are copying a table, you either need the destination table to already exist, or you need to create the table via a predetermined table build script. Is there a way of copying the source table to the destination regardless of what fields are in it?
(we could just change the SSIS package to reflect the changes, but it can take over a week for our DBA's to swap it out for us)
Any help would be greatly appreciated.
Thanks
oooo, the Jedi's will feel this one....
|
|
|
|
|
Paul Unsworth wrote: s there a way of copying the source table to the destination regardless of what fields are in it
Not to my knowledge and your DBAs will already have told you this
It is, via a LOT of design commitment create a package that reads the source structure and changes the destination structure but I would take a suit of armour when you approach the DBAs, they will first deny it is possible, then point out it is unreasonable and then quit if you insist. There is nothing a DBA hates more than someone screwing with their data structure.
I have done this when the business rule was that columns were only ever ADDED and never MOVED, but it was a right, royal PITA.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Haha. Fairy nuff. I suppose it makes sense. Although not quite what I want.
I suppose I'm just a control freak...
It's just our DBA's are as quick as geriatric slugs. Countless forms to fill in, just so that they can take a week to think about it, then either say no, hope that the requestee forgets about it, or say "yes. It will be operational in 2 weeks...". Can be a little frustrating.
I hope not all DBA's are like this. I may look into DB admin myself...
Thanks for you reply.
oooo, the Jedi's will feel this one....
|
|
|
|
|
Paul Unsworth wrote: I hope not all DBA's are like this
A crappy DBA is a developers nightmare, however the timeframe you find so irritating may be due to all the change management crap the DBA has to go through to effect an "Enterprise" level change. I work on departmental systems and do not need to conform to these rules thankfully.
This particular change, while challenging, goes against all their training so I would think you have buckleys of getting it done
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
i am trying to insert data or save data from language other than english in an access database.but it is giving me some error and not allowing me to save the same.
if anyone can find a solution please let me know as soon as possible.
Thanks in advance
|
|
|
|
|
- Can you show the code to insert/update?
- What error did you get?
I are troll
|
|
|
|
|
Hi Experts
I Have a database having only one table and this table has 12 Columns all Columns
have data type varchar(Max).Now i fill Grid By Searching Record on Particular Field
After The Result Come To me in grid i further filter the record in the incoming
result set that is come in grid.this filter process may me n -times so how i can
save the incoming result and filter them.the data inside the table is near about 100000 (1Lakhs) records.The Record search is also faster.
Currently i am using the SQL Server 2005 and C#.Net(2.0 Framework).
How i Make It Faster and Filter the record in n-pass. so i have not make round trip
on sql server every time when i search the record . i have display the record not to
save or update it in Database. i am not change my data type beacuse it store heavy
Data inside the single fields
Thanku
Dinesh Sharma
|
|
|
|
|
Instead of filtering your data in the grid, why not use a stored procedure (with parameters) to just return the records you are interested in?
|
|
|
|
|
Thanks
But Accutly Query Creating At Runtime User Can Search The Record in Any Fields.
Ok I am Agree With u i am making SP but the data come one time i filter using SP
but the next time i have filter data from the in coming result set not from whole database. i mean to say that where we can store result temp. so i can filter further
from in coming result accutly it a search engine software,so where we can store it.
|
|
|
|
|
Assuming you have some sort of applciation you could store the data from your first search locally and then filter that - in .net a datatable would be the thing to use, otherwise maybe some sort of collection?
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
pls send me example if possible
|
|
|
|
|
Without knowing anything about your application, including what language it is in and what type of application it is, how can I? Anyway, 2 minutes on google should give you plenty of examples, its a common enough thing to do.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
i have mention my tech. what i am using for you'r knowledge i am using sql server v2005
and VS 2005 .
|
|
|
|
|
OK, I had forgotten. Anyway, just google for datatable, dataadaptor and filter and you should be sorted, ther are hundreds of examples available.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Get all the data from the database once and store into DATASET.
Next by the help of DATAVIEW( http://www.csharp-examples.net/dataview-rowfilter/[^] ) you can filter the records.
You don't have to make N-number of round trips for that.
Hope this helps.
Niladri Biswas
|
|
|
|
|
I'm a bit of an SQL novice, so I have a feeling this is easy, but I can't figure it out and I'm Googled out trying to find the answer.
I have a query:
SELECT CONTACT.FIRSTNAME, CONTACT.LASTNAME, CONTACT.COMPANYNAME, CONTACT.USER2, EMAIL.ADDRESS
FROM dbo.CONTACT CONTACT, dbo.EMAIL EMAIL
WHERE CONTACT.CONTACTID = EMAIL.CONTACTID
ORDER BY CONTACT.LASTNAME DESC
That's fine, but contacts may have more than one email. So I might get, the following records as a result:
Ann Onymous Initech v7.35 ann.onymous@initech.com
Ann Onymous Initech v7.35 a.onymous@initech.com
Ann Onymous Initech v7.35 onymousa@initech.com
If I use Count(EMAIL.ADDRESS) I'd get 3 for Ann.
So how do I tell SQL to just get me the first email record and not the other two? e.g.
Ann Onymous Initech v7.35 ann.onymous@initech.com
I'm pretty sure its simple...but..so am I!
|
|
|
|
|
Like this:
SELECT CONTACT.FIRSTNAME, CONTACT.LASTNAME, CONTACT.COMPANYNAME, CONTACT.USER2, first(EMAIL.ADDRESS)
FROM dbo.CONTACT CONTACT
inner join dbo.EMAIL EMAIL on CONTACT.CONTACTID = EMAIL.CONTACTID
GROUP BY CONTACT.FIRSTNAME, CONTACT.LASTNAME, CONTACT.COMPANYNAME, CONTACT.USER2
ORDER BY CONTACT.LASTNAME DESC
|
|
|
|
|
I believe First() is an MSAccess function which in such a situation cannot be used when the DB is Sql Server
Niladri Biswas
|
|
|
|
|
You're right I'm querying SQL Server and First() doesn't work.
|
|
|
|
|
I don't know whether you are testing others or not but I guess you are so because you statement reveals that.
Any way, use the Top 1 statement of sql server to obtain the same.
SELECT TOP 1 CONTACT.FIRSTNAME, CONTACT.LASTNAME, CONTACT.COMPANYNAME, CONTACT.USER2, EMAIL.ADDRESS
FROM dbo.CONTACT CONTACT, dbo.EMAIL EMAIL
WHERE CONTACT.CONTACTID = EMAIL.CONTACTID
ORDER BY CONTACT.LASTNAME DESC
Hope this helps
Niladri Biswas
|
|
|
|