I have a stored proc I am running, and I would like to create a list of email addresses from a table and put that list into a variable. I did a basic while loop to work on syntax, but now I don't know how to actually get each address added on. Here's how I started it
declare @start int, @testEmail nvarchar(2000)
set @start = 1
set @testEmail = NULL
while @start <= (Select count(PADM_Email) from PADM_Emails)
Begin
--Print @start
set @testEmail = @testEmail + (Select distinct PADM_Email from PADM_Emails) + ';'
set @start = @start + 1
End
I know that the above is wrong, but I don't know how to get it right. Ideally, I want the @testEmail to look like this:
I would like to write a store prodecure to return a month:
My output: Wk1 = July Wk2 = July Wk3 = July Wk4 = July Wk5 = Aug
and so on..
then i create list of array like below:
The counter for insert the week one by one
DECLARE @TotalWeek INT, @counter INT DECLARE @WeekNo varchar, @Month varchar SET @WeekNo = '4,9,14,18,22,27,31,35,40,44,48,53' --this is weekno,if less than 4, month is july, lf less than 9, month is august and so on SET @TotalWeek = 53
I have a multiselect checkbox list in my UI. I pass the list items to my stored proc as comma separated parameter and then I have a function which converts this parameter to a table with separate rows.
E.g. : a,b,c,d
Converted to result table
result a b c d
I want to insert each row of the result table into another table. How to do that.
E.g., the table after the function is :
CREATE TABLE #result ( Subject varchar(100) )
insert into #result values ('a') insert into #result values ('b') insert into #result values ('c') insert into #result values ('d')
Hi,Right, I have this problem, and it;s more through lack of understanding vb.net that well more then an actual problem I will out line what I want to do,basically it all revolves around a page that needs to be built when navigated to so it can be easily updated without anyone having to edit the code.Get all the table names from a databaseLoop through each of the results to build a statementNest a 2nd loop to split the returned data from the correct tableBuild a listbox for each table returnedThis is what I have currently, this works but the problem is, if another course is added, someone will need to manually edit the code on the page to add a new code to get the new course hence why I want to create a loop that gets all the data so all someone needs to do is put in the all table the new course name. Please noteI cut this down to just show 2 result but there is about 30 odd. 1 DBConn = New SqlConnection("Server=SD02;Initial Catalog=WhoGetsWhat;Persist Security Info=True;UID=x;Password=xx") 2 'Dim DBDataAdapter As SqlDataAdapter 3 DBDataAdapter = New SqlDataAdapter("Select AOE,ADC, FROM TBL_Role WHERE Title = @ddlTitle", DBConn) 4 DBDataAdapter.SelectCommand.Parameters.Add("@ddlTitle", SqlDbType.NVarChar) 5 DBDataAdapter.SelectCommand.Parameters("@ddlTitle").Value = TitleDropDown.SelectedValue 6 DBDataAdapter.Fill(DSPageData, "Courses") 7 8 'Loop through each record, split at + and place in ListBoxs 9 10 VarDS = DSPageData.Tables("Courses").Rows(0).Item("AOE") 11 Dim VarArray As String() = VarDS.Split("+") 12 Dim i As Integer 13 For i = 0 To VarArray.Length - 1 14 15 Dim li As New ListItem() 16 li.Text = VarArray(i).ToString() 17 li.Value = VarArray(i).ToString() 18 Me.txtAOE.Items.Add(li) 19 Next i 20 21 VarDS = DSPageData.Tables("Courses").Rows(0).Item("ADC") 22 VarArray = Nothing 23 VarArray = VarDS.Split("+") 24 i = Nothing 25 For i = 0 To VarArray.Length - 1 26 27 Dim li As New ListItem() 28 li.Text = VarArray(i).ToString() 29 li.Value = VarArray(i).ToString() 30 Me.txtADC.Items.Add(li) 31 Next i Now here is my pseudo code to what I roughly want to do, hope it makes sense to someone and someone can point me in the correct direction. Please note,I know the split bit works, so at the minute I am just trying to get the loop to get all my courses 1 DBConn = New SqlConnection("Server=SD02;Initial Catalog=WhoGetsWhat;Persist Security Info=True;UID=wgw;PWD=wgwsql;") 2 DBSelect.Connection = DBConn 3 DBSelect.Connection.Open() 4 'Get the row number in the database 5 DBSelect.CommandText = "SELECT COUNT(*) FROM TBL_All" 6 DBResult = DBSelect.ExecuteScalar() 7 DBSelect.Connection.Close() 8 Dim Count = DBResult 9 'Get all the Tables and Keys in the Database's 10 DBDataAdapter = New SqlDataAdapter("SELECT * FROM TBL_All", DBConn) 11 DBDataAdapter.Fill(DSPageData, "All") 12 'declare all loop vars 13 Dim X As Integer 14 Dim Y As Integer 15 Dim i As Integer 16 'Loops through all the tables 17 Dim DSArray As String() = DSPageData.Tables("All").Items() 18 For Y = 0 To Count 19 Dim VarDS As String() = DSPageData.Tables("All").Rows(0).Item(DSArray(Y)) 20 Dim SplitArray As String() = VarDS.Split("+") 21 22 23 For i = 0 To SplitArray.Length - 1 24 Dim Li As New ListItem() 25 Li.Text = SplitArray(i).ToString() 26 Li.Value = SplitArray(i).ToString() 27 Me.txt & DSArray(Y) &.Items.Add(Li) 28 Next i 29 30 Next Y
I have a basic while loop but I need to be able to increment the counter from one alpha character to the next: declare @counter nvarchar(10) set @counter = 'A' while @counter < 'Z' beginprint 'the counter is ' + @counter set @counter = @counter + @counter end
In this example I would need to see the following output: the counter is Athe counter is Bthe counter is Cthe counter is D..... Is this possible in SQL?
Hey i have a table A that contains 3 columns : id, entry ,sessionid i want to create a view on this table that will contain - for each sessionid s in A --> select top 5 rows having s as sessionid and ordered by id desc (s can have 1 or 2 or 5 or 300 entries i want to get only the latest 5 rows that correspond to this session) I tried many queries and different combinations i could find one yet to do the following. Can anyone help me plz? Can we have a loop in a view?is it possible?
I need to run a script to create a trigger on 18 tables on 3 databases. The code is identical with the exception of the table and trigger names at the beginning. Does anyone know of a way to create them all with a loop instead of manually replacing the table and trigger names and executing over and over? I tried variables but get an 'Incorrect syntax near '@TriggerName' error.
if exists (select * from sysobjects where id = object_id (N'dbo.tgUsersAudit') and objectproperty (id, N'IsTrigger') = 1) drop trigger dbo.tgUsersAudit go
CREATE TRIGGER tgUsersAudit on tblUsers FOR insert, update, delete AS DECLARE @TableName varchar(128) SET @TableName = tblUsers
..................from here the code is the same for all
I'm new to sql and could do with some help resolving this issue.
My problem is as follows,
I have two tables a BomHeaders table and a BomComponents table which consists of all the components of the boms in the BomHeaders table.
The structure of BOMs means that BOMs reference BOMs within themselves and can potentially go down many levels:
In a simple form it would look like this:
LevelRef: BomA
1component A 1component B 1Bom D 1component C
What i would like to do is potentially create a temporary table which uses the BomReference as a parameter and will loop through the records and bring me back every component from every level
Which would in its simplest form look something like this
LevelRef: BomA
1......component A 1......component B 1......Bom D 2.........Component A 2.........Component C 2.........Bom C 3............Component F 3............Component Z 1......component C
I would like to report against this table on a regular basis for specific BomReferences and although I know some basic SQL this is a little more than at this point in time i'm capable of so any help or advice on the best method of tackling this problem would be greatly appreciated.
also i've created a bit of a diagram just in case my ideas weren't conveyed accurately.
I have a table with Year , Account and Amount as fields. I want to
SELECT Year, Account, sum(Amount) AS Amt
FROM GLTable
WHERE Year <= varYear
varYear being a variable which is each year from a query
SELECT Distinct Year FROM GLTable
My thought was that I would need to pass a variable into a select statement which then would be used as the source in my Data Flow Task.
What I have done is to defined two variables as follows
Name: varYear (this will hold the year)
Scope: Package
Data type: String
Name:vSQL (This will hold a SQL statement using the varYear)
Scope: Package
Data type: String
Value: "SELECT Year, Account, sum(Amount) AS Amount FROM GLTable WHERE Year <=" + @[User::varYear]
I've created a SQL Task as follows
Result set: Full Result Set
Connection Type: OLE DB
SQL Statement: SELECT DISTINCT Year FROM GLTable
Result Name: 0
Variable Name: User::varYear
Next I created a For Each Loop container with the following parameters
Enumerator: Foreach ADO Enumerator
ADO Object source Variable: User::varYear
Enumeration Mode: Rows in First Table
I then created a Data Flow Task in the Foreach Loop Container and as the source used OLE DB Source as follows
Data Access Mode: SQL Command from Variable
Variable Name: User::varYear
However this returns a couple of errors "Statement(s) could not be prepared."
and "Incorrect syntax near '='.".
I'm not sure what is wrong or if this is the right way to accomplish what I am trying to do. I got this from another thread "Passing Variables" started 15 Nov 2005.
I have a for each loop(ADO Enumerator) container which executes for each Advertiserid which is coming from database. In for each loop I have to create a new excel file with the advertiser name. So if the loop executes 7 times there should be seven excel spreadsheets with seven advertiser names.
How can i create an excel dynamically in the foreach loop container.
I am looking for the best way in SSIS to do the following. I have an SQL table that for each row in the table I want to take an element from the table do a lookup in a Teredata Table, return information from the teredata source. Use that returned data to do some calculations and create a derived column from my calculations and place the data into the same SQL table that I am parsing through.
SELECT FriendName from Friends where RegionId = 23 I would like to create a comma delimited list of 'FriendName' column values in above query (example - Mike,John,Lisa,Emburey). How would I do that?
Hi Guys, I just need a script to get all Job Id and Job Descriptions from Sql Server Agent/Jobs and then save it into a text file. I'm not sure how to accomplish this using Sql Server Management Objects(SMO). Any sample scripts and/or stored procedures, etc. would be appreciated. Thanks for your help. -W2528
I need to be able to set up a table listing users permissions, this needs to be run daily and then notify me of any changes without using the auditing or profiler software.
My SQL knowledge is limited to only trial-and-error and on-the-fly learning, sadly. However, I've had past experience with VB and some earlier, more basic programming, so I don't find it that difficult to grasp; it's just rather overwhelming without any formalized training.
I'm trying now to solve a problem that involves a sales history report. Our sales history table lists only records for product lines which a customer has purchased in the past. However, the budgeting team needs a list of each customer with all product lines, even if they've never purchased, and they'd like to include an industry code value that is from another table.
So the way the tables are looking is this:
Sales_History as the main table with the appropriate data in it Product_Lines as a reference to fill out the main table with non-purchased lines
Accounts as another reference that gives the industry code per the customer ID in the main table''I'm seemingly on the right track with the following, which tries to create a list of all product lines and the records from sales_history which match them:
SELECT sales_history.sah_sort_id,sales_history.sah_sortkey3,sales_history.sah_sortkey2_desc, prod_lines.pl_prodline_id,prod_lines.pl_short_desc FROM dbo.sales_history INNER JOIN prod_lines ON sales_history.sah_sortkey3=prod_lines.pl_prodline_id
[code]...
However, this produces a list of only 21 rows, when there should be 41.
Scenario:Table 1 (a id, b name)Table 2 (a FKid, d value)A standard join on a gives me something like:a1 b1 d1a1 b1 d2What I want is:a1 b1 d1,d2I can easily do this with a function or cursor, but is is somewhatslow, and I need to do this a lot and I don't really want to have tomaintain tons of functions or cursors.Thoughts?
I am trying to create a list of all the tables in one database and then list the size of each table. So for example I want to create a table with the table name and table size for one DB
E.g
Table1 1111KB Table2 123300MB Table3 120448KB
etc for all the tables in a particukar DB
I know there is a stored procedure to list the sizes: 'sp_spaceused' but not sure how to script all this together.
I am struggling trying to clean some data and identify duplicate records. I used fuzzy grouping in SSIS and provided back a series of groups. The issue is some of the individual records can appear in multiple groups (so in reality the groups should be combined). This is best explained with an example:
Original Data key1 key2 647942600014 647942285437 2324662490640 2324662285437 2324662066128 2222 2285437 2222 1111111 9999 1111111 9999 2222222
I only choose 2222 as the surviving key because it was the smallest number. I really do not care which number remains as long as it is the same across.
I tried playing with self joins between the tables but have had no success.
I am using Sql Server 2008 and the number of records could 500K to 1MM.
Hi,I'm sure this is a common problem.. to create a single field from awhole column, where each row would be separated by a comma.I can do this for a specified table, and column.. and I've created afunction using VBA to achieve a more dynamic (and very slow) solution..so I would like to implement it using a user defined function in sql server.The problems I'm facing are, that I can't use dynamic sql in afunction.. and I also can't use temporary tables which could build up a'standard' table from parameters given to then perform the function on.So, with these limitations, what other options do I have?Cheers,Chris
The above increments by 1 day which is defined in the 1st argument of the #duration. My question is how can I dynamically change the value of this 1st argument such that its the number of days in the current month hence it will increment to only return the 1st date in the Month e.g
1/1/2000 1/2/2000 1/3/2000 etc..
I prefer to use an elegant approach if possible, the alternative would be return all dates, create a custom column from these dates which returns the month date - delete the dates column - get a distinct list of the month dates.
What are the options to create a table of contents based on the report items in a List Control? Document Mapping works for online viewing. A table of content would make the report easier to read when it's printed.
I'm working to improve performance on a database I've inherited, and there are several thousand indexes. I've got a list of ones which should definitely exist within the database, and I'm looking to strip out all the others and start fresh, though this list is still quite large (1000 or so).
Is there a way I can remove all the indexes that are not in my list without too much trouble? I.e. without having to manually go through them all individually. The list is currently in a csv file.
I'm looking to either automate the removal of indexes not in the list, or possibly to generate the Create statements for the indexes on the list and simply remove all indexes and then run these statements.
As an aside, when trying to list all indexes in the database, I've found various scripts to do this, but found they all seem to produce differing results. What is the best script to list all indexes?
Hi, looking for some help on job notification emails in SQL 2005.
I've created a backup job in the Maintenance Plan section of SQL2005, and added a "Notify Operator" task to notify me when the job completes. I've setup Database Mail, and I am getting the notification email whenever I run the job. So this is working fine.
The problem is when I got to the 'Jobs' folder, and into the properties of my backup job, I set it up to notify me from the notifications option, but I am not getting any emails. For now I have set it to notify me when the job "succeeds".
Any ideas why it works from the Maintenance Plan section, but not the "Jobs" notification section? Am I forgetting something?
I have recently migrated a SQL2000 instance to SQL2005, and would now like to setup all the existing jobs to notify me if they fail.
Hi I am using asp.net framwork 2 with sql 2005 express edition. Can anyone please provide me any sample of sending automated emails (like welcoming people when they register to my website, or if their personal details have been updated....). or any e-book/website ref will do. I am not very expert in sql/db and smtp (or other email) stuffs. So please help me. With thanks
How would I go about checking incoming e-mails? For example, on a certain e-mail address, I would get e-mails formatted in a certain way. According to the response, some scripts need to run/ some sql tables updates etc. How can one do this in (ASP) .NET with SQL Server? Anyone did this kind of stuff before?
I'm new to programming and to databases so apologies if this sounds like a stupid question.
I have a html page with a simple javascript function that uses the href mailto tag to send an email to a normal email address. I was wondering if it would be posssible to send the email directly to an SQL Server database, without having to pass through an intermediate parsing app. It would mean assigning an email address to the database. Is this possible with SQL Server? I know there would still be problems with parsing the email content to the appropriate fields, but would the sending of the email to the database in itself be possible?
I haven't much experience using databases and I don't currently have access to the SQL Server Express database that will be used to store the email content, so I'm not sure how difficult an operation this would be. I've looked on the internet but I haven't been able to find anything. I was hoping someone here might have done something similar, either with SQL Server or with any other database.
I'm trying to remove emails from a table but the simple update query I wrote doesn't seem to do it. I figured I'd test one record instead of the entire dbase. I've probably given more info than needed (the select statement) but wanted to give a frame of reference.
SELECT Name.ID, EMAIL, COMPANY, MEMBER_TYPE FROM APSCU_PROD.dbo.Name
Hi, folks. I'm fairly new to SQL Server, so forgive any stupidityinherent in this question. I work at a university and a certainfaculty member would like to have her students create a simple auctionweb site as an exercise. As part of the assignment, she wants theauction site to be able to send an email to a bidder when they havebeen outbid.So she has asked me if this is possible, and how to do it. I've read abit about sending an automated email from SQL Server, but I can't findanything about how to trigger the email based upon a field comparisonin the database. I also don't know how to incorporate a field value asa variable into the body of the email.Of course, I don't expect anyone to solve my entire problem in thisforum. I was just hoping that someone could point me in the rightdirection. Can anyone give me any advice about where I should belooking for such info? Can you recommend a web page, or some otherreference material? Is there a specific terminology I should be usingwhen I search? I just don't seem to be able to find what I need.Please reply to Join Bytes! -- I don't read newsgroups regularly.Many thanks in advance!--Sue